jQuery Validation Weirdness
I am using jQuery 1.4.2, and I am trying to validate an e-mail address when one is inputted into a form. The e开发者_开发百科mail field is not required so blanks can be passed; however, jQuery evaluates the field as not empty even when it is left blank. I'm new to jQuery, so I'm probably overlooking something simple. Offending code is below.
var
emailTest,
re = /* RegEx here for email addresses */
if ($(formID + ' input[name=email]').val() != '') {
emailTest = re.test($(formID + ' input[name=email]').val());
}
alert($(formID + ' input[name=email]').val() != '');
returns false
when the email value is left blank, which doesn't make any sense.
Use
if ( ! $(formID + ' input[name=email]').val().length === 0 ) {
//your code
}
精彩评论