简体   繁体   中英

Jquery form validation issue

I'm using jQuery Validate for my site's contact form. The input fields have pre-populated default values, ie “Name” is the default value for the text field where people should type their name.

Some of these fields are required, but the basic set-up of jQuery Validate only checks if there is content in the fields, not what the content is. My problem is that even if people don't enter their name, the form will still validate because the default value of “Name” still exists in the field.

Does anyone know how I can program jQuery validate to check for the default values, and if they exist, to return an error message?

Thanks for the help!

I would use the valid = function(inputValue){ return true; } valid = function(inputValue){ return true; }

From the docs

Set the values that are considered valid when the input is checked. The function returns true for valid input and false for invalid input.

So you would do something like

$('myform').validate({ valid: function(input){
    // check input value against your default values
    // - If match, return false (is NOT valid)
    // - If no match, return true (is valid)
});

This is a really good tutorial I've used to validate forms ajax style. http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html

It'll give you more control over what you validating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM