开发者

javascript validation - ignore default field value

trying to ignore the default value (used a field hint for accessibility) of a field when the form is going

through it's validation.

The validation is in javascript:

      if (giftCardNumber >= 'xxxxxxxxxxxxxxx' && giftCardNumber <= 'xxxxxxxxxxxxxxxxx' ) {
     giftAlert (incdpy_msg_065,"frmGCD_"+formIndex+"_Number");
     if ((typeof TeaLeaf != "undefined") && (typeof TeaL开发者_如何学Pythoneaf.Client != "undefined") && (typeof TeaLeaf.Client.tlAddEvent != "undefined") )  {    // Tealeaf Include
          var nVO = { ErrorMessage :incdpy_msg_065}
          var subtype="CustomErrorMsg";
          TeaLeaf.Event.tlAddCustomEvent(subtype, nVO);
     }

     return false;
   }

I assume it's a fairly simple line - if(giftCardnumber === "Enter gift card")...

xxx's are for security reasons.

Or can I just clear all default values on focus and on submit?


You can just check against the defaultValue property:

if (element.value == element.defaultValue) {
  // it has its default value
} else {
  // it has some value other than its default
}

The usual way is to use onfocus and if value == defaultValue, set the value to '' (empty string). Then onblur, do the opposite (i.e. if value == '', set to defaultValue).


Yes - if you know what the default value is, and can validate against it, this is the most straightforward way of doing it.


You basically need to test for that value yes. Then it depends if you want to prevent the form from being submitted or send an empty value to the server.

Did you try that and encounter a problem ?

edit: As you say clearing the values on focus would be nice (for the user) but not sufficient since the user can still submit the form without 'touching' the field. In the submit you can just do

if(giftCardNumber === "default value") {
  giftCardNumberField.value = "";
}

Provided you have access to the field this will simply send an empty value to the server. This will also have the annoying habit of leaving the field blank (ugly if the page does not get submitted because the form was invalid or because you do it through ajax).

Actually I don't know what's the best practice for this. Looking at the ext docs I see that their Textfield will just submit its empty text if given an html name.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜