Monday, July 22, 2013

Regular Expression validation for frontend(JS) and backend(PHP)

Start with one example:

For validate Name Field: Alphabets, numbers and space(' ') no special characters min 3 and max 20 characters.

var ck_name = /^[A-Za-z0-9 ]{3,20}$/; 
if (!ck_name.test(name))
{
        alert('Enter valid Name.');
        return false;
}


Note: The above code for front end using JavaScript. If you are using PHP use key word preg_match instead of test.

Some important conditions:
[abc] Find any character between the brackets
[^abc] Find any character not between the brackets
[0-9] Find any digit from 0 to 9
[A-Z] Find any character from uppercase A to uppercase Z
[a-z] Find any character from lowercase a to lowercase z
{3,10} Check for the string length in between give boundary.


Important Links:


No comments:

Post a Comment

Learn JavaScript - String and its methods - 16

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>String and it's methods - JS&l...