Monday 9 December 2013

Email and Phone Validation

Suppose you have created a picklist named Type where you are giving an option of selecting Email or Phone and entering the value in text field and want to validate that text field depending on the value of value selected in picklist.

You can achieve this by creating a simple validation rule

You have Two fields
  1. Type : Picklist value with values
  • Business Email Address
  • Private Email Address
  • Home Phone
  • Mobile Phone
  • Work Phone 
  2. Value : Text Field

All you have to do is to create a validation rule on Value

















function used:

IF(logical_test, value_if_true, value_if_false): 
Checks whether a condition is true, and returns one value if TRUE and another value if FALSE.

ISPICKVAL(picklist_field, text_literal) :
Checks whether the value of a picklist field is equal to a string literal

REGEX(Text, RegEx_Text) :
Returns TRUE if Text matches the regular expression RegEx_Text. Otherwise, it returns FALSE.

NOT(logical) :
Changes FALSE to TRUE or TRUE to FALSE

Now i have used
ISPICKVAL(Type__c, 'Business Email Address') || ISPICKVAL(Type__c, 'Private Email Address')

which means picklist value selected is 'Business Email Address' OR 'Private Email Address'

if above statement( ISPICKVAL(Type__c, 'Business Email Address') || ISPICKVAL(Type__c, 'Private Email Address') ) is true then 

Not(REGEX( Value__c , "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,7}"))
 
will execute which will check for correct email format, if format not matched then it will throw custom error message.

but if statement( ISPICKVAL(Type__c, 'Business Email Address') || ISPICKVAL(Type__c, 'Private Email Address') ) is false 
then 
Not(REGEX( Value__c , "[0-9]{2,5}")
will execute which will check the number/phone format


Note: can change the length by changing the value in curly braces
eg Not(REGEX( Value__c , "[0-9]{2,5}")
will check for length 2 to 5

eg Not(REGEX( Value__c , "[0-9]{2,7}")
will check for length minimum 2 to maximum 7  

No comments:

Post a Comment