JavaScript test()方法更改返回值

时间:2019-05-31 06:45:03

标签: javascript jquery typescript

我正在编写一个小型表单验证,由于某种原因,当我调用de test()方法时,它返回true;如果不更改输入值,则在再次调用该方法时,它返回{{ 1}},然后再次false,依此类推。

这是true中的形式,处于形式形式:

html

这是函数的片段:

<form class="modal-content" (submit)="dashboard.altaCliente()">
  <div class="input-group">
    <input type="text" name="" id="clienteNombre" placeholder="Nombre" class="form-control">
    <input type="text" name="" id="clienteApellido" placeholder="Apellidos" class="form-control">
  </div>
  <input type="email" name="" id="clienteEmail" placeholder="Correo electronico" class="form-control my-2">
  <div class="input-group">
    <input type="text" name="" id="clienteDNI" placeholder="DNI" class="form-control">
    <input type="date" name="" id="clienteDate" placeholder="Fecha de nacimiento" class="form-control">
  </div>
  <input type="password" name="" id="clientePasswd" placeholder="Contraseña" class="form-control mt-2">
  <div class="mt-2 alert" role="alert"></div>
  <button type="submit" class="btn btn-primary">Añadir cliente</button>
</form>

现在,我仅验证姓名和姓氏。

模式是typeScript的属性,并且是:

altaCliente() { const $inputNombre = $("#clienteNombre"); const $inputApellidos = $("#clienteApellido"); const $inputEmail = $("#clienteEmail"); const $inputDNI = $("#clienteDNI"); const $inputDate = $("#clienteDate"); const $inputPasswd = $("#clientePasswd"); const $msg = $(".alert"); let todoOK = true; $msg.html(""); $inputNombre.on("focus",function(){$(this).removeClass("is-invalid")}); $inputApellidos.on("focus",function(){$(this).removeClass("is-invalid")}); $inputEmail.on("focus",function(){$(this).removeClass("is-invalid")}); $inputDNI.on("focus",function(){$(this).removeClass("is-invalid")}); $inputDate.on("focus",function(){$(this).removeClass("is-invalid")}); $inputPasswd.on("focus",function(){$(this).removeClass("is-invalid")}); //Nombre if ($inputNombre.val() === "") { $msg.html($msg.html() + "Nombre vacio<br>").addClass(["alert-danger", "show"]); $inputNombre.addClass("is-invalid"); todoOK = false; } else if (!this.regexNombre.test($inputNombre.val().toString())) { $msg.html($msg.html() + "Nombre mal formado<br>").addClass(["alert-danger", "show"]); $inputNombre.addClass("is-invalid"); todoOK = false; } //Apellidos if ($inputApellidos.val() === "") { $msg.html($msg.html() + "Apellidos vacios<br>").addClass(["alert-danger", "show"]); $inputApellidos.addClass("is-invalid"); todoOK = false; } else if (!this.regexApellidos.test(<string>$inputApellidos.val())) { $msg.html($msg.html() + "Apellidos mal formados<br>").addClass(["alert-danger", "show"]); $inputApellidos.addClass("is-invalid"); todoOK = false; } if (todoOK) { $msg.html("Usuario añadido con éxito<br>").removeClass("alert-danger").addClass(["alert-success", "show"]); } }

问题出在/^[aA-zZñÑ]+( [aA-zZñÑ]+)*$/g上,正如我所说的,else if返回test(),然后在再次调用该函数时返回true

为清楚起见,例如,如果我在名称输入中输入“ John”,然后单击“发送”按钮,则它将在第一次有效,在第二次无效,再次在第三次有效,依此类推。

1 个答案:

答案 0 :(得分:0)

所以我最终发现,在正则表达式模式末尾删除'g'可以解决问题。

使用/^[aA-zZñÑ]+( [aA-zZñÑ]+)*$/g,返回值在true和false之间波动,但是/^[aA-zZñÑ]+( [aA-zZñÑ]+)*$/正常工作。