+ Start a Discussion
anillllanillll 

Mobile number should not take alphabets and special characters

I have written this code but its not taking

String phoneNumber = eachMob.Contact_Number_Mobile__c;
        Pattern isnumbers = Pattern.Compile('^[0-9]+$');
           Matcher MobileMatch = isnumbers.matcher(phoneNumber);

             if(MobileMatch.Matches())
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)

Hi Anil,

This (http://forceguru.blogspot.com/2011/06/using-regex-in-validations.html) post might help you

Thanks,
N.J

Raghu Reddy 14Raghu Reddy 14
Hi Anil,

You can also use Javascript. 

<apex:inputField value="{!whatever.Phone}" id="phone">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>

var phoneVal = j$('[id$=phone]').val();
if((validatePhoneNumber(phoneVal))
{
  // do something
} else {

  // do anything

}

function validatePhoneNumber(phnNumber)
{ 
  var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/; 
  return phoneNumberPattern.test(phnNumber); 
}



</script>




/Raghu