简体   繁体   English

JS中电话号码的自定义验证

[英]Custom validation for phone number in JS

I am trying to add phone validation on form submit.我正在尝试在表单提交时添加电话验证。 Main goal is: if phone starts not on "39" or "+39" add this numbers before the number and submit.主要目标是:如果手机不是以“39”或“+39”开头,请在号码前添加此号码并提交。 Code is added via script manager just after body tag.代码是通过脚本管理器在 body 标签之后添加的。 But it doesn't work on submit.但它不适用于提交。 And I don't have any errors in browser console.而且我在浏览器控制台中没有任何错误。

<form action="some.php" method="POST">
  <div class="fields">
    <div class="lp-pom-form-field single-line-text" id="container_nome">
      <label class="main lp-form-label" for="nome" id="label_nome" style="height: auto;">
        <span class="label-style">Nome e Cognome&nbsp;*</span>
      </label>
      <input type="text" id="nome" name="nome" placeholder="" class="ub-input-item single text form_elem_nome">
    </div>
    <div class="lp-pom-form-field single-line-text" id="container_telefono">
      <label class="main lp-form-label" for="telefono" id="label_telefono" style="height: auto;">
        <span class="label-style">Telefono&nbsp;*</span>
      </label>
      <input type="text" id="telefono" name="telefono" placeholder="" class="ub-input-item single text form_elem_telefono">
    </div>
  </div>
</form>

 window.onload = function() {
      var mainForm = document.forms[0];
      mainForm.addEventListener("submit", function validPhone() {
        var re = /^\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d$/;
        var myPhone = '';
        var element = document.getElementById('telefono');
        if (element != null) {
          myPhone = element.value;
        }
        else {
          myPhone = null;
        }
        var myPhoneTrim = myPhone.trim().replace(/\s|\-|\(|\)/g, "");
        var valid = re.test(myPhoneTrim);

        if (myPhoneTrim.startsWith("39")) {
          myPhoneTrim = "+" + myPhoneTrim;
        } else if (!myPhoneTrim.startsWith("39") && !myPhoneTrim.startsWith("+39")) {
          myPhoneTrim = "+39" + myPhoneTrim;
        }
        return valid;
      });
    }

Try using string.match()尝试使用string.match()

var matches = string.match(^39); instead of if (myPhoneTrim.startsWith("39"))而不是if (myPhoneTrim.startsWith("39"))

And var matches = string.match(^[+]39);var matches = string.match(^[+]39);

However, I do not see a problem with your initial code.但是,我认为您的初始代码没有问题。 Can you please post the HTML?你能把HTML贴出来吗?

Not sure what your HTML looks like or how you apply the JS script, but running this in JSFiddle seems fine: https://jsfiddle.net/7u2h3nqn/17/不确定您的 HTML 是什么样子或您如何应用 JS 脚本,但在 JSFiddle 中运行它似乎没问题: https ://jsfiddle.net/7u2h3nqn/17/

JS: JS:

var mainForm = document.getElementById("lp-pom-button-205");

mainForm.addEventListener("click", function validPhone() {

        var re = /^\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d$/;
        var myPhone = '';
        var element = document.getElementById('telefono');

        if (element != null) {
          myPhone = element.value;
        }
        else {
          myPhone = null;
        }
        var myPhoneTrim = myPhone.trim().replace(/\s|\-|\(|\)/g, "");
        var valid = re.test(myPhoneTrim);

        if (myPhoneTrim.startsWith("39")) {
          myPhoneTrim = "+" + myPhoneTrim;
        } else if (!myPhoneTrim.startsWith("39") && !myPhoneTrim.startsWith("+39")) {
          myPhoneTrim = "+39" + myPhoneTrim;
        }

        console.log(myPhoneTrim);

        return valid;
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM