首页 > 解决方案 > 联系表格“姓名”字段验证问题

问题描述

我是 PHP 新手,一直在拼凑我自己的联系表格......

在下面的场景 2 中,我无法让表单不验证。代码也在下面..

场景 1:输入 2 个字母或数字 = 弹出错误且邮件未发送。

场景 2:输入了 3+ 个数字 = 弹出错误,但邮件确实发送。

$(document).ready(function(){
$("#contact_email").focus(function(){
  if($("#contact_name").val().length < 3){
    $("#contact_name").css("border", "solid 1px #D295B5");
  }else {
    if(allLetter($("#contact_name").val())){
      $("#contact_name").css("border", "solid 1px #D295B5");
    }else {
      $("#contact_name").css("border", "solid 1px #DADB93");
    }
  }
});


function allLetter(inputtxt){
var letters = /^[A-Z a-z]+$/;
if(inputtxt.match(letters)){return false;}
else{return true;}
}

有什么想法是我想念的吗?这样,如果在名称字段中输入了任何数字,它将不会发送。

标签: jquery

解决方案


这听起来很傻,但我发现有时你放置函数的顺序会影响脚本的逻辑输出。所以,试着把你的function allLetter(inputtxt)代码放在你的上面$(document).ready(function()

function allLetter(inputtxt){
var letters = /^[A-Z a-z]+$/;
if(inputtxt.match(letters)){return false;}
else{return true;}
}

$(document).ready(function(){
$("#contact_email").focus(function(){
  if($("#contact_name").val().length < 3){
    $("#contact_name").css("border", "solid 1px #D295B5");
  }else {
    if(allLetter($("#contact_name").val())){
      $("#contact_name").css("border", "solid 1px #D295B5");
    }else {
      $("#contact_name").css("border", "solid 1px #DADB93");
    }
  }
});

这将允许函数在调用时位于缓存中。


推荐阅读