Javascript:通过正则表达式验证表单字段并使用if / else语句

时间:2017-10-22 03:24:59

标签: javascript

在我的程序中,我已经在表单中创建了表单和字段。我正在尝试验证字段。为此,我使用了正则表达式虽然我的正则表达式是正确的但电话的正则表达式没有执行。任何人都可以告诉我,我是否正确定义if / else语句进行验证?

代码:



function validateForm() {
  var zip = document.myform.zip;
  var email = document.myform.email;
  var phone = document.myform.phone;
  if (email.value == "") {
    window.alert("Please enter email Address.");
    state.focus();
    return false;
  }

  var regEx2 = /^[0-9]{5}(?:-[0-9]{4})?$/;

  if (zip.value.match(regEx2)) {
    return true;
  } else {
    window.alert("Please provide a zip in the ##### or #####-#### format.");
    zip.focus();
    return false;
  }

  var regEx = /^(?:\(\d{3}\)\s|\d{3}-)\d{3}-\d{4}$/;

  if (phone.value.match(regEx)) {
    return true;
  } else {
    window.alert("Please enter Telephone number in (xxx) xxx-xxxx format.");
    phone.focus();
    return false;
  }


}

<form name="myform" id="myform" action="" onsubmit="return validateForm()" method="post">
  <table cellspacing="2" cellpadding="2" border="0">
    <tr>
      <td align="right">Zip</td>
      <td><input type="text" name="zip" maxlength="15" size="28" /></td>
    </tr>
    <tr>
      <td align="right">Telephone number</td>
      <td><input type="text" name="phone" size="28" placeholder="(555) 555-5555" /></td>
    </tr>
    <tr>
      <td align="right"> EMail </td>
      <td> <input type="text" name="email" size="28" /></td>
    </tr>
    <tr>
      <td align="right"></td>
      <td><input type="submit" value="Submit" /> </td>
    </tr>
  </table>
&#13;
&#13;
&#13;    

1 个答案:

答案 0 :(得分:2)

问题出在这里 -

module.exports = mongoose.model('Product',{
  imagePath: {type: String, required: true},
  title: {type: String, required: true},
  description: {type: String, required: true},
  price: {type: Number, required: true},
  variants: [ProductVariant]
});

无论zip是有效还是无效,您都会从这一点返回。之后的代码块将无法执行。

我建议不要在每个成功的 if block 中返回,因为代码执行将从该点返回而不验证其他字段。

 if (zip.value.match(regEx2)) {
    return true;
  } else {
    window.alert("Please provide a zip in the ##### or #####-#### format.");
    zip.focus();
    return false;
  }

如果您确实只想在所有以前的字段有效时(作为当前代码)验证一个字段,那么这是有效的 -

       function validateForm() {

            var valid = true;
            var zip = document.myform.zip;
            var email = document.myform.email;
            var phone = document.myform.phone; 

            if (email.value == "")
            {
                window.alert("Please enter email Address.");
                email.focus();
                valid = false;
            }   

            var regEx2 = \^[0-9]{5}(?:-[0-9]{4})?$\;

            if(!zip.value.match(regEx2))
            {
                window.alert( "Please provide a zip in the ##### or #####-#### format." );
                zip.focus() ;
                valid = false;
            }  

            var regEx = \^(?:\(\d{3}\)\s|\d{3}-)\d{3}-\d{4}$\;

            if(!phone.value.match(regEx)) 
            {  
                window.alert("Please enter Telephone number in (xxx) xxx-xxxx format.");
                phone.focus();
                valid = false;  
            }  

            return valid;
       } 

这是完整的脚本 -

         function validateForm() {

            var zip = document.myform.zip;
            var email = document.myform.email;
            var phone = document.myform.phone; 

            if (email.value == "")
            {
                window.alert("Please enter email Address.");
                email.focus();
                return false;
            }   

            var regEx2 = \^[0-9]{5}(?:-[0-9]{4})?$\;

            if(!zip.value.match(regEx2))
            {
                window.alert( "Please provide a zip in the ##### or #####-#### format." );
                zip.focus() ;
                return false;
            }  

            var regEx = \^(?:\(\d{3}\)\s|\d{3}-)\d{3}-\d{4}$\;

            if(!phone.value.match(regEx)) 
            {  
                window.alert("Please enter Telephone number in (xxx) xxx-xxxx format.");
                phone.focus();
                return false;
            }  

            return true;
         }