开发者

default values in input fields getting past validation on submission

I wrote a small validation script (partially as an exercise in learning) but I can't figure out how to make it detect default values in the input fields. It seems like I need to store the default value in 开发者_运维百科a variable and then add another OR in there to check if the current value matches that default value but I can't quite figure out how to do that. I can't put the variable inside the function executed on the click because then it takes the value at that point and uses that. Any help would be appreciated. I thought about just using "placeholder" but I wanted to make it work in IE. thanks in advance.

$('.submit').click(function() {
var ret_val = true;
var emailaddressVal = $("#email").val();
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
$('.required').each(function(){
if($(this).val().length === 0 || $(this).find("option:selected").val() == "none") 
{
    $('#errormessage').removeClass('requiredmessage');
    ret_val = false;
    $(this).addClass('warning');
}
else if($(this).val().length != 0)
{
    $(this).removeClass('warning');
}
});
if(!emailReg.test(emailaddressVal))
{
    $('#emailerrormessage').removeClass('requiredmessage');
    $('#email').addClass('warning');
    ret_val = false;
}
else
{
    $('#emailerrormessage').addClass('requiredmessage');
}
return ret_val;
});


You can check for...

var isDefault = $(this).val() === $(this).prop('defaultValue');

Keep in mind if you are repopulating the value attributes after an unsuccessful submission to your server, this will give false positives (depending on your business rules, of course).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜