简体   繁体   English

哪个JavaScript库可用于客户端表单检查?

[英]What JavaScript library to use for client-side form checking?

Over the years, I've dabbled in plain JavaScript a little, but have not yet used any JavaScript/AJAX libraries. 多年以来,我已经尝试了一些简单的JavaScript,但尚未使用任何JavaScript / AJAX库。 For some new stuff I'm working on, I would like to use a js library to do client-side form validation, and want to know which would be best for that. 对于我正在研究的一些新东西,我想使用一个js库来进行客户端表单验证,并想知道哪种方法最适合。 By best, my criteria would be: quick and easy to learn, small footprint, compatible with all popular browsers. 最好的情况是,我的标准是:快速且易于学习,占用空间小,与所有流行的浏览器兼容。

Edit: Thanks for the ASP suggestions, but they're not relevant to me. 编辑:感谢您的ASP建议,但它们与我无关。 Sorry I didn't originally mention it, but the server is a Linux box running Apache and PHP. 抱歉,我最初没有提到它,但是服务器是运行Apache和PHP的Linux机器。 As I know I should, I plan to do server side validation of the input, but want the client side validation to improve the users' experience and to avoid as much as possible having the server reject invalid inputs. 据我所知,我计划对输入进行服务器端验证,但是希望客户端验证可以改善用户体验,并尽可能避免服务器拒绝无效输入。

Edit 2: Sorry I didn't reply for months! 编辑2:对不起,我好几个月没回复了! Other priorities came up and diverted me from this. 其他优先事项浮出水面,这使我偏离了这一点。 I ended up doing my own validation routines - in addition to the good points made in some of the answers, some of the items I'm validating are rarely used in other applications and I couldn't find a library with that sort of validation included. 我最终做了自己的验证例程-除了某些答案中指出的要点外,我要验证的某些项目很少在其他应用程序中使用,并且我找不到包含这种验证的库。

您可以使用jQuery及其Validation插件

I don't use libraries myself, but dived into some (like prototype, (yui-)ext, the seemingly omnipresent jquery, mootools) to learn from them and extract some of the functions or patterns they offer. 我自己不使用库,而是潜入了一些库(例如原型,(yui-)ext,看似无所不在的jquery,mootools)以从库中学习并提取它们提供的某些功能或模式。 The libraries (aka 'frameworks') contain a lot of functionallity I never need, so I wrote my own subset of functions. 库(又称“框架”)包含许多我不需要的功能,因此我编写了自己的函数子集。 Form checking is pretty difficult to standardize (except perhaps for things like phone numbers or e-mail address fields), so I don't think a framework would help there either. 表单检查非常难以标准化(也许除了电话号码或电子邮件地址字段之类的东西以外),因此我也不认为框架可以帮助实现这一点。 My advice would be to check if one of the libraries offer the functionallity you look for, and/or use/rewrite/copy the functions you can use from them. 我的建议是检查其中一个库是否提供您要查找的功能,和/或使用/重写/复制您可以从中使用的功能。 For most open source libraries it is possible to download the uncompressed source. 对于大多数开源库,可以下载未压缩的源。

It needs to be said (by the way and perhaps well known allready) that client side form checking is regarded insufficient. 需要说(顺便说一句,也许众所周知),客户端表单检查被认为是不够的。 You'll have to check the input server side too. 您还必须检查输入服务器端。

Before AJAX Libraries I used Validation.JS by Matthew "Matt" Frank. 在AJAX库之前,我使用过Matthew“ Matt” Frank的Validation.JS。

The basic idea is that you include a JS file and then add attributes to your INPUT statement. 基本思想是先包含一个JS文件,然后将属性添加到INPUT语句中。

Example: 例:

<input name="start-date" type="text" 
    display-name="Start Date" date="MM/YYYY" required="@getRequired()" />

Field will be validated as a date in MM/YYYY style. 字段将被验证为MM / YYYY样式的日期。 Any error message displayed will refer to the field as "Start Date". 显示的任何错误消息均将字段称为“开始日期”。 The "@" prefix will cause the getRequired() function to be evaluated at run-time. “ @”前缀将导致在运行时评估getRequired()函数。

A variety of things are provided as standard (Currency, Date, Phone, ZIP, Min/Max value, Max length, etc), and there is a keystroke filter; 作为标准,提供了各种各样的功能(货币,日期,电话,ZIP,最小/最大值,最大长度等),并且有一个按键过滤器; alternatively you can roll your own - most easily by just defining a Regular Expression for the field, but you can add Javascript Functions to be called to make the validation. 另外,您也可以自己滚动-仅通过为字段定义正则表达式即可,但是可以添加要调用的Javascript函数进行验证。

There are pseudo events for handlers to catch before/after field and form. 处理程序具有伪事件,以捕获字段和表单之前/之后。

In additional to Attributes in the INPUT statement, validation actions can be applied to the field by JS: 除了INPUT语句中的Attributes之外,JS还可将验证操作应用于该字段:

// Set field background when in error state
document.MyForm["INVALID-COLOR"]="yellow";

// Show error messages on field blur
document.MyForm["SUPPRESS-ONCHANGE-MESSAGE"]=true;

document.MyForm.MyField.REQUIRED = true;
document.MyForm.MyField.DisplayName="Password";

Validation.JS is 28K (uncompressed) Validation.JS为28K(未压缩)

I've had a bit of a trawl around to try to find an HTML file you can easily get to with details, but I can't fine one standalone that I can link to. 我四处搜寻,试图找到可以轻松获取详细信息的HTML文件,但是我无法对一个可以链接的独立文件进行罚款。

The source code is here: 源代码在这里:

http://code.google.com/p/javascript-form-validation/source/browse/#svn/trunk http://code.google.com/p/javascript-form-validation/source/browse/#svn/trunk

and the DOCs are in the HTML files - but you can't view those as HTML, you have to download them and then view them, as far as I can make out 并且DOC在HTML文件中-但您无法将它们以HTML格式查看,必须下载并查看它们,据我所知

I do most new stuff in ASP.NET with AJAX, so I use the ASP.NET validators with the AJAX extenders, and they work great. 我使用AJAX在ASP.NET中进行大多数新工作,因此我将ASP.NET验证器与AJAX扩展程序一起使用,它们工作得很好。 However, if you are not into ASP.NET this isn't going to help you. 但是,如果您不喜欢ASP.NET,这将无法为您提供帮助。

Most major JavaScript frameworks (jQuery, YUI, Prototype, etc) have validation capabilities, so you could consider them. 大多数主要的JavaScript框架(jQuery,YUI,Prototype等)都具有验证功能,因此您可以考虑使用它们。 But depending on your needs, you might regard it as overkill. 但是,根据您的需要,您可能会认为这是过度杀伤力。

Previously (in ASP Classic) I used my own validation script which was only 6KB; 以前(在ASP Classic中),我使用了自己的验证脚本,该脚本只有6KB。 I obviously don't now because I like the consistency and polish offered by these frameworks, but YMMV. 我现在显然不喜欢,因为我喜欢这些框架提供的一致性和完善性,但是我喜欢YMMV。

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

相关问题 我应该使用哪个JavaScript库进行客户端连字? - Which JavaScript library should I use for client-side hyphenation? 简单的 Javascript 客户端表单验证 - Simple Javascript client-side form validation 使用JavaScript进行客户端表单验证 - Client-side form validation with javascript 填写客户端表单[Javascript] - Filling in a form client-side [Javascript] javascript-我的客户端选项是什么? - javascript - What are my client-side options? 可以将ExactTarget与客户端JavaScript一起使用吗? - Is it possible to use ExactTarget with client-side JavaScript? 您是否对WebSocket使用任何客户端javascript库或jQuery插件? - Do you use any client-side javascript library or jQuery plugin for WebSockets? 将变量从服务器端控制器传递到客户端,以在客户端JavaScript中使用 - Pass variable from server-side controller to client-side to use in client-side JavaScript 如何今天打包客户端Javascript库? - How to package a client-side Javascript library today? 是否有一个javascript客户端库,用于排队具有存储支持的ajax请求 - Is there a javascript client-side library for queuing ajax requests with storage support
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM