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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用