RSS

Javascript validations using Regular expression

04 May

Just call the methods,  It will validate and return the true/false.

1.Email validation

function email_check(giv_email){
     var EMAIL_REGEX = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
     var email_value = EMAIL_REGEX.test(giv_email); //return true or false
     return email_value;
}

1.Phone Number Validation
2.Not allow speical character and alphabets

function phone_no_check(giv_number){
     var PHONE_REGEX = /^[0-9]+$/;
     var phone_value = PHONE_REGEX.test(new String(giv_number)); //return true or false
     return phone_value;
}

1.Website Validation (with out http://)
2.This will validate (www.test.com)

function website_check(giv_website){
     var WEBSITE_REGEX = /^([a-z0-9_-]+\.)*[a-z0-9_-]+(\.[a-z]{2,6}){1,2}$/;
     var website_value = WEBSITE_REGEX.test(giv_website); //return true or false
     return website_value;
}

1.Website validation (with http://)
2.This will validate (http://www.test.com)

function url_check(giv_url){
     var URL_REGEX = /^(https?|ftp):\/\/.*$/i;
     var url_value = URL_REGEX.test(giv_url); //return true or false
     return url_value;
}

1.Check string and number only
2.Special character are not allowed

function string_name_and_number_check(giv_name){
     var NAME_REGEX = /^[A-Za-z0-9 ]{3,35}$/;
     var website_name_value = NAME_REGEX.test(giv_name); //return true or false
     return website_name_value;
}

1. Check string only
2. Numeric and special character are not alowed

function string_check(giv_str){
     var STRING_REGEX = /^[A-Za-z ]{3,35}$/;
     var string_value = STRING_REGEX.test(giv_str);
     return string_value;
}

1. Initial letter number not allowed other we will allow number
2. only “_” simple is allowed (Its like sql table name validation)

function strSqlTableName(givStr){
     var STRING_REGEX = /^[a-zA-Z][a-zA-Z0-9_]*$/;
     var stringvalue = STRING_REGEX.test(givStr); //return true or false
     return stringValue;
}

1. Not allow speical character
2. Other language characters allowed.
3. Allowed numbers

function strNameWithNumeric(givStr){
     var STRING_REGEX = /^[\w\u4e00-\u9eff\.]{1,20}$/;
     var stringvalue = STRING_REGEX.test(givStr); //return true or false
     return stringValue;
}

1. Not allow speical character and Allowed numbers
2. Other language characters allowed.

function strNameWithOutNumeric(givStr){
     var STRING_REGEX = /^[\-a-zA-Z\_\u4e00-\u9eff\s]{1,100}$/;
     stringvalue = STRING_REGEX.test(givStr); //return true or false
     return stringValue;
}

1. Date validation (DD/MM/YYYY)

function dateValidation(givStr){
     var dob_regex = /^([0-9]){2}(\/){1}([0-9]){2}(\/)([0-9]){4}$/;   // DD/MM/YYYY
     stringvalue = STRING_REGEX.test(givStr); //return true or false
     return stringValue;
}

 
Leave a comment

Posted by on May 4, 2011 in Javascript, Strings

 

Tags: , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: