Spring JS - 输入字段

问题描述 投票:0回答:1

我在.jsp文件中有两个输入字段,两个输入字段同时可见。 phone1是强制性的,并且按预期工作(必须按照格式填写,长度为10)。但是phone2不工作;我希望phone2遵循与phone1相同的规则,但仅在输入字段中输入内容时才通知问题。

恩。如果在phone2中输入了一个字母或只有一个数字,请将元素文本设置为:

“*请输入一个没有特殊字符的10位数字”

    var phoneformat = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/;

      //phone1="home phone" phone2="work phone"

      if (phone1 == "" || phone1.length != 10 || !phone1.match(phoneformat)){
        document.getElementById("vHphone").innerHTML="*please enter a 10 digit number with no special characters";
        return false;
    } else if (phone2 != "" && phone2.length < 10 || !phone2.match(phoneformat)){
        document.getElementById("vWphone").innerHTML="*please enter a 10 digit number with no special characters";
        return false;

我该如何解决这个问题?

javascript spring eclipse hibernate jsp
1个回答
0
投票

您也可以尝试将phone2 != ""添加到当前or运算符的右侧。否则,它仅针对!phone2.match(phoneformat)触发。

else if (phone2 != "" && phone2.length < 10 || phone2 != "" && !phone2.match(phoneformat))

要么

else if(phone2 != ""){
  if(phone2.length < 10 || !phone2.match(phoneformat)){
     return false;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.