douchenzhan3050 2018-01-21 18:49
浏览 22
已采纳

使用jquery验证非必需输入的问题

Using JQuery, I want to validate non required input field if input field value exists.

value allow null in DB

<input type="text" class="form-control" name="moms_md_name" id="moms_md_name" value="">

Using JQuery

var mom_maiden_name = $('#moms_md_name').val();

1rst try

// …
} else if (maiden_name !== '' && maiden_name !== null) {
  if (!maiden_name.match(/^(?=.*[a-z]).{3,20}$/)) {
    alert("Mother maiden name not properly entered!");
  }
// …

Now when I:

  • Leave the field blank/empty, it works and I get result
  • Disable JQuery validation I get result too
  • Enter a wrong input like 222 or aa, I get error message
  • Enter a correct input, I get no result at all <- ?

2nd try

// …
 } else if(maiden_name !== '' && maiden_name !== null){
   if (
      $('#moms_md_name').val()
      && !mom_maiden_name.match(/^(?=.*[a-z]).{3,20}$/)) {
          alert("Mother maiden name not properly entered!");
   }
// …

Now when I:

  • Leave the field blank/empty, it works and I get result
  • Disable JQuery validation I get result too
  • Enter a wrong input like 222 or aa, I get error message
  • Enter a correct input, I get no result at all <- ?

PHP - looking for something equivalent of below (disable for testing)

// …
else if(!empty($_POST['moms_md_name'])) {
  if (
    isset($_POST['moms_md_name'])
    && !preg_match('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['moms_md_name'])
    ) {
        echo "Mother maiden name need to be properly entered!";
        exit();
    }
}
// …

Specifying a valid value gives no message/console log.
Though it works if I disable JQuery and enable PHP validation.

  • 写回答

1条回答 默认 最新

  • douban2014 2018-01-21 19:28
    关注

    I'll begin with your Javascript code:

        else if (maiden_name !== '' && maiden_name !== null) {
          if (!maiden_name.match(/^(?=.*[a-z]).{3,20}$/)) {
             alert("Mother maiden name not properly entered!");
       }
    

    First you should verify if the value is empty then do your code:

    var mom_maiden_name = $('#moms_md_name').val();
    if (mom_maiden_name  == '' || mom_maiden_name.match(/^(?=.*[a-z]).{3,20}$/)) {
        //Validation passed 
    } else {
        //Validation failed again
    }
    

    I'm not sure what the regex should do but I assume it's correct.

    Second, looking at your PHP code, I see a wrong validation there. If you first check a value for not being empty, you can automatically be assured that it's set. If you want to keep your code, you should invert those 2 conditions in 'if' :

    if(isset($_POST['moms_md_name'])) {
       if (!empty($_POST['moms_md_name']) && !preg_match('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['moms_md_name'])) {
          echo "Mother maiden name need to be properly entered!";
          exit();
       }
    } 
    

    Again, I don't know what the regex does so I will assume it's correct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了