开发者

jQuery Validation Weirdness

开发者 https://www.devze.com 2023-02-16 03:19 出处:网络
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 evaluat

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消