jquery - 联系表格“姓名”字段验证问题
问题描述
我是 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;}
}
有什么想法是我想念的吗?这样,如果在名称字段中输入了任何数字,它将不会发送。
解决方案
这听起来很傻,但我发现有时你放置函数的顺序会影响脚本的逻辑输出。所以,试着把你的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");
}
}
});
这将允许函数在调用时位于缓存中。
推荐阅读
- google-cloud-platform - SaaS和PaaS可以一起使用吗?
- react-native - 在浏览器中使用时,汇总的 UMD 模块输出为空对象
- python - 如何根据条件创建列
- assembly - MIPS 忽略“Beq”语句
- javascript - 文本框的占位符值不会更改为原始值?
- android - 找不到将按钮添加到 TToolBar 的新按钮选项
- python - 向绘图添加正确的 Yticks
- python - Django 没有检测到我的模型中的变化
- c# - 如何在我的 ASP Net Core 5.0 项目中比较“ß”和“ss”?
- sql - SQL Server 计算数据窗口中的聚合差异