Sunday, January 14, 2018

User Defined Function to validate Regular expression in javascript

A Regular expressions is a powerful tool in any language. In this tutorial we will be discussing how to use regular expression using Jquery and javascript. A regular expression is an object that describes a pattern of characters.
Regular expression validation using JQuery

In another word Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text. We have created few function which covers different type of validation in HTML Form.

These Function return true and false during the function call.
True - When it is comply with validation expression 
False - When it is not comply with validation expression 


Regular Expression to validate website URL

This function used to validate website URL.
//function used to validate website URL
function checkUrl(url)
{
    //regular expression for URL
    var pattern = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/;
 
    if(pattern.test(url)){
        return true;
    } else {
        return false;
    }
}


Regular Expression to validate username

This function used to validate username that accept 5-15 characters without any special characters
//function used to validate username
function checkUsername(username){
 //regular expression for username
    var pattern = /^[a-z0-9_-]{5,15}$/;
    if(pattern.test(username)){
        return true;
    }else{
        return false;
    }
}


Regular Expression to validate email address

This function used to check valid email address.
//function used to check valid email
function checkEmail(email)
{
    //regular expression for email
    var pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if(pattern.test(email)){
        return true;
    } else {
        return false;
    }
}

Also you can use below pattern for email id validation:
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

Regular Expression to validate user password

This function used to validate password. This validation makes the user password very strong and which complies below format :
  • Must be at least 8 characters
  • At least 1 special character from @#$%&
  • At least 1 number, 1 lowercase, 1 uppercase letter

//function used to validate password
function checkPassword(password){
 //regular expression for password
    var pattern = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%&]).*$/;
    if(pattern.test(password)){
        return true;
    }else{
        return false;
    }
}

Regular Expression to validate user password with minimal validation.
This another example for user password validation, here we are validating user password which complied below format.

  • Accept 5-15 characters
  • Accept alphabets and numbers

// This function validate User password...
function checkPassword(password){
    var pattern = /^[a-zA-Z0-9_-]{5,15}$/;
    if(pattern.test(password)){
        return true;
    }else{
        return false;
    }
}


Regular Expression to validate Mobile Number

This function used to validate mobile number of user. It will accept only10 digit mobile number.
//function used to validate mobile number
function checkMobileNumber(mobile){
 //regular expression for mobile number
    var pattern = /^[0-9]{10}$/;
    if(pattern.test(mobile)){
        return true;
    }else{
        return false;
    }
}

This is all about regular expression validation using jquery and javascript. Please do comments in comment box, in case you have any questions.



No comments:

Post a Comment