Javascript JS验证没有';在我输入有效输入并提交表单(其余输入字段为空)后,t运行
我正在开发一个web目录,其中一个页面上有一个表单。使用JS验证插件。在提交表单而不填写任何输入字段时,表单会按预期在每个输入字段下方抛出错误!但是,提交表单时只需填写一个输入框即可刷新当前页面,因为操作值被设置为包含PHP代码的当前页面,而不是停留在同一页面上,继续为其余尚未填写的字段抛出错误!我在网上搜索,没有找到任何有用的东西来找出脚本的错误。这里的任何人都可以查看下面的代码并推荐最佳解决方案吗?谢谢Javascript JS验证没有';在我输入有效输入并提交表单(其余输入字段为空)后,t运行,javascript,php,jquery,jquery-validate,Javascript,Php,Jquery,Jquery Validate,我正在开发一个web目录,其中一个页面上有一个表单。使用JS验证插件。在提交表单而不填写任何输入字段时,表单会按预期在每个输入字段下方抛出错误!但是,提交表单时只需填写一个输入框即可刷新当前页面,因为操作值被设置为包含PHP代码的当前页面,而不是停留在同一页面上,继续为其余尚未填写的字段抛出错误!我在网上搜索,没有找到任何有用的东西来找出脚本的错误。这里的任何人都可以查看下面的代码并推荐最佳解决方案吗?谢谢 $(document).ready(function() { $("#user
$(document).ready(function() {
$("#userForm").validate({
rules: {
cname: {
required: true,
lettersonly: true,
minlength: 3
},
cemail: {
required: true,
email: true
},
cphone: {
required: true,
number: true,
minlength: 10,
maxlength: 10
},
cbusiness: {
required: true,
url: true
},
cbcategory: {
required: true,
minlength: 6
},
curl: {
required: true,
minlength: 6
},
},
messages: {
cname: "Please enter your name",
cemail: "Please enter a valid email address",
cphone: {
required: "Please enter your phone number",
number: "Please enter only numeric value"
},
cbusiness: {
required: "Please enter your business",
},
cbcategory: {
required: "Please enter a business category",
},
curl: {
required: "Please enter the URL to your website",
},
}
});
});
表格如下
<form action="" method="post" name="userForm" id="userForm">
<input type="text" name="cname" id="cname" placeholder=" Your Name">
<input type="text" name="cemail" id="cemail" class="email" placeholder="Your Email">
<input type="text" name="cphone" id="cphone" placeholder="Your Phone">
<input type="text" name="cbusiness" id="cbusiness" class="email" placeholder=" Your Business">
<input type="text" name="cbcategory" id="cbcategory" placeholder="Business category">
<input type="text" name="curl" id="curl" class="email" placeholder="URL"><br>
<label for='message'>Enter the code in the box below : </label>
<img src="captcha.php?rand=<?php echo rand();?>" id='captchaimg'>
<input type="text" id="captcha_code" name="captcha_code">
<input type="submit" name="Submit" id="Submit" value="Submit" class="button1"><br>
Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh.
</form>
在下面的框中输入代码:
“id='captchaimg'>
无法读取图像?单击以刷新。
假设您正确地包含了验证库,则必须为所有验证类型设置消息,如:
messages: {
cname: {
required: "Please enter your name",
lettersonly: "Letters only",
minlength: "Min length 3 required"
},
cemail: {
required: "Please enter a valid email address",
email: "Invalid email"
}
}
工作
您必须包含以下插件文件:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/additional-methods.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>
请编辑问题并添加您想要的内容和您需要的内容get@lonut,感谢您的建议。我已按建议设置了邮件,但问题仍未解决。例如,当我在“姓名”字段中键入数字时,错误消息不会从“请输入姓名”更改为“仅限字母”。所有输入字段的错误消息似乎都停留在“必需的错误消息”处。还有什么建议吗?@Pramod,对不起,我错了。在进行调试时,我忘记为名称添加所需的lettersonly和minlength选项。我已经更新了答案中小提琴的链接。现在请看。您可以在JSFIDLE中看到它正在工作。如果在您的项目中没有工作,请检查您的控制台是否有错误。@lonut。谢谢。我想我们快到了。我做的和你看了JSFIDLE后做的完全一样。当我在name字段中输入数字时,它没有抛出“仅字母错误”。其他字段似乎都接受了它们的用途。@Pramod,不客气。很乐意帮忙。你说
我想我们就快到了。
那么还有什么不对的吗?你还需要帮助吗?@lonut,你帮了大忙。通过验证,我现在成功了90%。我发现我需要确保错误消息对应于无效的数据用户输入。名称字段的情况是1。如果输入2个字母,它会显示“请输入您的姓名”,而不是min 3。2.如果输入数字为123654,它仍然会显示“请输入您的姓名”,而不是仅显示字母。3.当我输入正确的姓名为Pramod时,它仍然会显示消息“请输入您的姓名”。再次感谢您抽出时间。
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");