0

I have 2 custom validation methods in my JSP which one of them has regex on the ID format entered, and the other method which help me to search whether the user has existed in the database.

Would like to ask how do I call my 2 custom validation methods inside my jquery validation plugin method (as seen below) before the form is submitted? Currently the validation only checks if the fields are empty. I would like to also check against the pattern of string entered (validateIDNo()) and if the user exists (checkUser(id)) before submitting the form.

Thank you so much.

function validateIDNo(){
//regex goes here, returns true/false
}

function checkUser(id){
//search database for ID, returns true/false
}

<script type="text/javascript">
    //jquery validation Script
    var _global_validator = null;

    $(document).ready(
        function() {
            $("#form1").validate( {
                errorElement : "small",
                //ignore:".ignore",             
                invalidHandler : function(e, validator) {
                    _global_validator = validator;
                    renderCheckableUIValidation(validator)
            },

            errorPlacement : function(error, element) {
                error.insertAfter(element);
            },

            messages : {
                idNo: "Please enter your ID number",
                    mobileNo: "Please enter your mobile number",
                    emailAdd: "Please enter your email address"
            },

                submitHandler: function(form) {
                form.submit();
            }

        }); //end validate
    }); 

</script>
Sparky
  • 98,165
  • 25
  • 199
  • 285
ah gal
  • 279
  • 2
  • 7
  • 19
  • The validation should not run before user input have been made. – Roman C May 03 '15 at 15:18
  • hi. i am only running the validations when submitting the form. i just want to know how to call the custom methods inside the jquery validate script. – ah gal May 03 '15 at 15:19
  • you can do that using [`onkeypress()`](http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeypress) event of javascript – Abhishek Ghosh May 03 '15 at 15:21
  • 1
    Answer is in the docs http://jqueryvalidation.org/jQuery.validator.addMethod/ – charlietfl May 03 '15 at 15:23
  • 1
    Look at `blur` - When the user leaves the input, for example, you could run the validation on that part of your form – Cory May 03 '15 at 15:23
  • Everything you're asking about is already automatically handled internally by the plugin. Simply follow the documentation for creating a custom rule and you won't have to worry about triggering it with any special handlers. – Sparky May 04 '15 at 05:59

0 Answers0