Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript form validation

    • 0
    • 1
    • 2
    • 1
    • 0
    • 0
    • 0
    • 0
    • 291
    Comment on it

    Simple Javascript Form Validation

    Why we need javascript validation :

    1: To validate user input on client side.

    2: Force user to enter required data.

    3: Remove dependency on server side language to validate data for primary validation (which is not required database manipulations ).

    4: For good user experience.

    *Note Every Programmer should Include Server Side Validation In Their Program. Because Javascript Validation Are Not Secure. Any Good Programmer Can Skip Javascript Validation On The Fly :) .

    Here We Go!

    /**
     * Javascript FormValidation Class 
     * @author Naveen Butola
     * Working @ Evon Technologies
     * Version 0.1
     */
    
    var FormValidation = function() {}
    
    FormValidation.prototype.isEmpty = function(MyValue) {
        if (MyValue == "" || MyValue.length == 0) {
            return true;
        } else {
            return false;
        }
    }
    
    FormValidation.prototype.isNumeric = function(MyValue) {
        var ValExpression = /^[0-9]+$/;
        if (MyValue.match(ValExpression)) {
            return true;
        } else {
            return false;
        }
    }
    
    FormValidation.prototype.isAlphabet = function(MyValue) {
        var ValExpression = /^[a-zA-Z]+$/;
        if (MyValue.match(ValExpression)) {
            return true;
        } else {
            return false;
        }
    }
    
    FormValidation.prototype.isAlphabetWithSpace = function(MyValue) {
        var ValExpression = /^[a-zA-Z\-]+$/;
        if (MyValue.match(ValExpression)) {
            return false;
        } else {
            return true;
        }
    }
    
    FormValidation.prototype.isSpecialChar = function(MyValue) {
        var ValExpression = /[_~`!@#\$%\^&\*\(\)\-_\+=\\/|,"'><\.:;\?]+/;
        if (MyValue.match(ValExpression)) {
            return true;
        } else {
            return false;
        }
    }
    
    FormValidation.prototype.isEmail = function(Email) {
        var ValExpression = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        if (Email.match(ValExpression)) {
            return true;
        } else {
            return false;
        }
    
    }
    
    FormValidation.prototype.isBothEqual = function(Password1, Password2) {
    
        var n = Password1.localeCompare(Password2);
        if (n == 0) {
            return true;
        } else {
            return false;
        }
    
    }
    
    FormValidation.prototype.isURL = function(Url) {
    
        var ValExpression = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
    
        if (!ValExpression.test(Url)) {
            return false;
        } else {
            return true;
        }
    
    }
    
    
    /*
     * How to use FormValidation class
     * <script>
     * var formValiObject = new FormValidation();
     * alert(formValiObject.isEmail("naveen.butola@evontech.com"));
     * alert(formValiObject.isBothEqual("naveen","Naveen"));
     * </script>
     */
    

    That's it.

    Feel Free To Contact Me @ naveen.butola@evontech.com for any query and suggestions or any query related this blog you can also post it in comment section of the same blog.

    Please find the attached FormValidation.js file for reference

    Have a good day.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: