简体   繁体   English

jQuery验证未捕获ReferenceError

[英]jQuery validation Uncaught ReferenceError

I have a form with multiple inputs and I want to validate the required ones. 我有一个包含多个输入的表单,我想验证所需的输入。

container.all("input").each(function(){
if(this.hasAttribute("required")){
        if((this.val()==="") {
            undentifiedFunction()
        }
    }

(By mistake) undentifiedFunction() is not defined, and it valides the inputs as I wanted. (错误)unidentifiedFunction()未定义,并根据需要验证输入。 How should I do it the right way? 我应该如何正确地做呢?

You may want to take a step back for a second & see the overall picture of form validation. 您可能需要退后一步,并查看表单验证的整体情况。 Asking for an easy code snippet here is going to cause problems for you, until you understand what it is that you really need to do first. 在这里要求简单的代码段会给您带来麻烦,直到您了解真正需要做的是什么。

You're going to want to validate each input field & if it fails, then you'll want to highlight that input with a red border & optionally turn on a text message below the input explaining what's wrong. 您将要验证每个输入字段,如果输入失败,则将用红色边框突出显示该输入,并有选择地打开输入下方的文本消息,以说明问题所在。 The idea is to be as user friendly as possible to an end-user who is not a programmer. 这个想法是对不是程序员的最终用户尽可能地友好。

Users will need to know that credit card number fields can't contain letters. 用户将需要知道信用卡号字段不能包含字母。 Or a zip code field can't be 3 characters long. 或邮政编码字段不能为3个字符长。 Or a phone number field can't be 5 digits long. 或电话号码字段的长度不能为5位数字。 They may try to add those characters or accidentally do something wrong on a long form (think: airline reservation sites). 他们可能会尝试添加这些字符或在长格式上意外地做错了事(请考虑:航空公司预订网站)。 So you could make a loooooong list of if/else-if/else or switch/case statements for all of your validation rules, but there is a better way of doing form validation. 因此,您可以为所有验证规则列出if / else-if / else或switch / case语句的清单,但是有一种进行表单验证的更好方法。

What I recommend for client-side work is to build a JSON object of regular expressions, which check legitimate patterns of valid input field values. 我建议客户端进行的工作是构建一个正则表达式的JSON对象,该对象检查有效输入字段值的合法模式。 The RegEx strings will determine if the user's input is valid or invalid, based on the string rules. RegEx字符串将根据字符串规则确定用户输入的有效还是无效。 For invalids, you can flag the fields by adding an ".invalid" class name. 对于无效对象,可以通过添加“ .invalid”类名称来标记字段。 You could also use jQuery to turn on any specific error messages. 您也可以使用jQuery打开任何特定的错误消息。 You'll still want to build server-side validation, but at least pre-built RegEx values are easy to find online & will help simplify your validation, so that you don't have to maintain giant conditional blocks of code (if/else-if/else & switch/case statements). 您仍然需要构建服务器端验证,但是至少可以在网上轻松找到预先构建的RegEx值,这将有助于简化验证,因此您不必维护庞大的条件代码块(如果/否则-if / else和switch / case语句)。 It's easy to miss validation rules in conditional blocks, but the RegEx statements will catch them. 在条件块中错过验证规则很容易,但是RegEx语句会捕获它们。

So, it may not matter what people think that the "right way" is. 因此,人们认为“正确的方法”是什么并不重要。 Everyone will have a different opinion. 每个人都会有不同的意见。 Whatever works for you & your site is the right way for you . 无论哪种方式适合您,您的网站都是适合您正确方法 It's better to think critically about what you're building, so that you learn the skills to build form validation more easily in the future. 最好对所构建的内容进行批判性思考,以便将来学习更轻松地构建表单验证的技能。

Start by looking at this RegEx site's examples section: http://www.regular-expressions.info Or search the web for pre-built RegEx strings for email addresses, phone numbers, IP addresses, zip codes, state abbreviations, credit card number formats, etc... You can test them out with online tools like: http://regexr.com or offline tools like: RegEx Builder, which is over at: https://sourceforge.net/projects/regexbuilder/ 首先查看此RegEx网站的示例部分: http : //www.regular-expressions.info或在网络上搜索预先构建的RegEx字符串,以获取电子邮件地址,电话号码,IP地址,邮政编码,州缩写词,信用卡号格式等...您可以使用在线工具(例如: http ://regexr.com)或离线工具(例如:RegEx Builder)对其进行测试,该工具位于: https//sourceforge.net/projects/regexbuilder/

Here is an example, which I built a few years ago: http://www.eonline.com/resources/js/libs/eol/eol.regEx.js 这是我几年前构建的示例: http : //www.eonline.com/resources/js/libs/eol/eol.regEx.js

I'd build that differently today, but it has the .match() function + a starter JSON RegEx object. 我今天将以不同的方式构建它,但是它具有.match()函数+入门JSON RegEx对象。 It showcases how complex form validation pattern matching is. 它展示了表单验证模式匹配的复杂程度。 Oh and you'll want to make sure to keep junk input values out of your database. 哦,您将要确保将垃圾输入值排除在数据库之外。 Emails like: blah@blah.com or a state name like "XY" or a first name like "Darth" and last name like "Vader". 电子邮件,例如:blah@blah.com或州名(如“ XY”)或名(如“ Darth”)和姓(如“ Vader”)。 Good luck! 祝好运!

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

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