[转载]集成最强的表单验证的 GTAjax

原文地址http://wadelau.net/gtajax/

GTAjax / 通用Ajax ( LastUpdated: Mon, 29 Jan 2007 15:27:39 GMT )

Author: Wade Lau, wadelau@ufqi.com, wadelau@hotmail.com

Summary / 概述
GTAjax is General-Targeted Ajax. Ajax stands for Asynchronous JavaScript And XML.
GTAjax is a kind of Ajax, and beyond the Ajax.
Maybe you have see and have used it in some coding experience. And also, you have not found any inefficent, actually, common Ajax has its own limits in many ways, It depends on the environment heavily.
Mostly, you code one Ajax which can not run in another place with differences in Browser,Operating System,Bankend programming components,etc…
In order to comply with the differences, people creates many kinds of Ajax, amd so do I, luckily enough, I found one generally-targeted way to comply with all the differences… , I call it GTAjax. Another reason for this name is GTAjax also refers to GridTask Ajax, based on gridtask.com .

GTAjax 就是通用(General-Targeted)Ajax的意思。是Ajax的一种. Ajax是异步Javascript和XML应用的合称。 可能你已经见到和使用过Ajax的应用,并且还没有发现它有什么不足。实际上,Ajax本身有很多限制,它对环境的依赖很强。
大多数情况是你在一处写的Ajax无法在别的地方使用,它对浏览器,操作系统和后台程序都有不同的限制和要求。
人 们为了完成各样环境下的Ajax应用,编写不了不同的Ajax版本,我也一样,幸运的是我找到一个可以通行应用于不同环境下的Ajax,我叫它 GTAjax。 取这个名字的另外一个原因是,GTAjax 也代表 GridTask Ajax 的意思,因为GTAjax就产生在 gridtask.com 。

Feathers / 特征
GTAjax的特征表现在:
1. 具有Ajax的功用,可替代Ajax的工作
2. 其通用性表现在:
a. 与操作系统无关,这应是javascript的本性
b. 与浏览器无关,目前我们已经在MSIE,Mozilla firefox,Opera上测试通过
c. 与Web服务器无关, 不依赖于Web服务器的特质去完成任务,可在流行的Apache,IIS等Web server运营的站点使用
d. 与后台脚本应用程序无关, 也即针对后台服务器脚本asp,php,jsp,perl等无须做改动
3. 对Ajax的硬伤–文件上传作了单独的优化处理
4. 代码精简,仅有7k左右 , (带表单验证有12K)
5. 使用简便,可调用操作,设置不同运行参数
6. 无需设置服务器端,这一点听起来很荒谬
7. 表单验证(v1.58+)
GTAjax’s feathers include:
1. do what Ajax can do
2. its general feather as following:
a. no mater what OS , inherits from javascript
b. no matter what Browser, it has been tested pass in MSIE, firefox, Opera
c. no matter what web server, it can used in Apache, IIS, etc
d. no matter what backend programming language, it can be used with jsp, asp, php, perl,etc
3. for Ajax’s shortcoming–file upload, it has been optimised to handle files upload correctly
4. small size, only about 7k , (with form validator up to 12k)
5. easy to use, with parameters to set for specified running
6. no action needed with server, which sounds unreasonable
7. form validator (v1.58+)

Usage / 用法
在您的网页代码中加入:[place the following code in your page] ….
<script type=”text/javascript” src=”GTAjax_.js”></script>
….

然后写出自己的业务处理函数[then, write you own functions]: ….

function subMyForm(sFrm)
{
// before submiting…
setGTAjax(‘returnname’,’Back’); // do settings 设置环境变量
var getTmpStr = GTAjax(sFrm); // main action 调用主函数处理并取结果
// after submited…
}

GTAjax不但能递交表单,也可处理url请求 [also,GTAjax could handle url request]
function handleMyUrl(sUrl)
{
// before go to next url….
setGTAjax(‘targetarea’,’newdiv’);// 设置将从服务器端取到的内容显示到新区域
//setGTAjax(‘returndataonly’,true);
setGTAjax(‘iscache’,false); // 设置是否处理缓存,更多设置见附后参考[with more settings in the following part]
var getTmpStr = GTAjax(sUrl); // 主处理函数
// after get remote material…
}
….

接着将这些动作绑定到对象去[then, bind the function to your target]: ….

<span id=”formArea”>(请将form放到一个对象中去,place form in a named object)
<br/>
<form name=”myform” id=”myform” action=”thisform.do.php” method=”post” onsubmit=”return false;”>(禁用默认递交,disable the default submit, 1.69+不再做要求)
<input type=”text” id=”userName” name=”userName” value=”Wade Lau”/><br/>
….
<input type=”submit” value=”Submit” id=”btn1″ name=”btn1″ onClick=”javascript:subMyForm(this.form.name);”/>(这里的参数是表单名,here give function the form’s name)<br/>
</form>
</span>

ok,done.现在,开始吧[now, it is time to go] ….

Reference and Download / 参考和下载
@ GTAjax运行时配置参考[settings references]: ….
setGTAjax(‘subbtnval’,’Submit’); // 表单递交按钮显示的字符,默认是Submit
setGTAjax(‘maxuploadfiletime’,3*60*1000); // 上传文件等待回应的最大时间,默认是3分钟
setGTAjax(‘targetarea’,’areaname’); // 递交请求获取数据后写在当前页面的何处,默认是递交元素所在的区域
setGTAjax(‘returnname’,’Return’); // 表单递交后,返回链接的显示字符,默认是Return
setGTAjax(‘returndataonly’,false); // 是否仅仅取得返回数据(而不向目标区域写入),默认是false
setGTAjax(‘iscache’,true); // 是否缓存从服务器取得的内容,默认是true
setGTAjax(‘forceframe’,false); // 是否强制使用iframe技术,即便没有fileupload行为也不使用xmlhttp,在一些xmlhttp受限的地方可使用iframe代替
setGTAjax (‘processbar’,true); // 是否显示处理中提示条, 默认是显示 (1.89+, idea from ayg at founder dot com)
setGTAjax (‘backlink’,true); // 是否在返回内容中追加返回链接,默认为是(1.89+ )
setGTAjax(‘forceframetag’,’fftag’); //告知服务器端,此次请求是普通请求,非xmlhttp,服务器端可据此fftag=1来判断(1.91+)
setGTAjax(‘chkform’,’userMail:email+:user mail should be an valid email address’); // 设定表单递交验证项(详细见下表)
….

GTAjax的表单验证,其配置为:
setGTAjax(‘chkform’,’userMail:email+:user mail should be an valid email address’);
其中’chkform’ : tell GTAjax to validate form data.
‘userMail: email+: user mail should be an valid email address’
‘strFormFieldName:formValidateType: errorMessage’
GTAjax Form Validation Descriptors // GTAjax 表单验证关键字描述 formValidateType Explainations[partly from http://www.javascript-coder.com ]
required
req The field should not be empty // 不能为空
maxlen=???
maxlength=??? checks the length entered data to the maximum. For example, if the maximum size permitted is 25, give the validation descriptor as “maxlen=25″ // 长度检测,至多允可长度, 如欲限定在25字符内,则使用表达式, “maxlen=25″, 在GTAjax中的完整表达为
setGTAjax(‘chkform’,’strField:maxlen=25:this field max length is 25.’);
minlen=???
minlength=??? checks the length of the entered string to the required minimum. example “minlen=5″ //最小限定为
alphanumeric
alnum Check the data if it contains any other characters other than alphabetic or numeric characters //仅为字母和数字(不含符号等)
num
numeric Check numeric data // 仅为数字
alpha
alphabetic Check alphabetic data. // 仅为字母
email The field is an email field and verify the validity of the data. // 限为合法email地址
lt=???
lessthan=??? Verify the data to be less than the value passed. Valid only for numeric fields.
example: if the value should be less than 1000 give validation description as “lt=1000″ // 其值不小于
gt=???
greaterthan=??? Verify the data to be greater than the value passed. Valid only for numeric fields.
example: if the value should be greater than 10 give validation description as “gt=10″ // 其值不大于
regexp=??? Check with a regular expression the value should match the regular expression.
example: “regexp=^[A-Za-z]{1,20}$” allow up to 20 alphabetic characters. // 其值符合给定的正则表达式
dontselect=??

Deprecated This validation descriptor is valid only for select input items (lists) Normally, the select list boxes will have one item saying ‘Select One’ or some thing like that. The user should select an option other than this option. If the index of this option is 0, the validation description should be “dontselect=0″ // 这个是 http://www.javascript-coder.com 的标配, 其本身局限大,功能小,舍弃
notvalue=???

newlyadded Verify the data to be any data other than the specified value // 是上面一个的的升级和扩容,是一个反向概括,如不能让其值为空”notvalue=” , 不能选择第一个选项”notvalue=1″,用户名一项的值不能等于初始值”notvalue=用户名”…
(anyofabove)+

newlyadded the “+” means req, or required, not empty when added up to any type of validating above, e.g. “email+” which means the expected value is an email address, and the field also is required, not empty. // 在写表单验证时参考了很多目前流行的,这个就是精简的结果,比如其中一项,即要符合email地址规范,又不能为空,则 “email+” , 其中一项只能是数字,且不能留空,则 “num+”,…

< 下载/Download
2006.10.12, GTAjax/通用Ajax-1.91
2006.09.22, GTAjax/通用Ajax-1.89
2006.07.25, GTAjax/通用Ajax-1.86
2006.07.07, GTAjax/通用Ajax-1.72
2006.05.23, GTAjax后端脚本之aspx
2006.05.23, GTAjax后端脚本之php
2006.03.22, GTAjax/通用Ajax-1.69
2006.03.15, GTAjax/通用Ajax-1.58, with form validator.
2006.03.12, GTAjax/通用Ajax-1.36
2006.02.17, GTAjax/通用Ajax-1.28

Thanks and Declarations / 致谢与声明
I wanna say THANKS :
to my home, they do more and spare me to have time code these. 感谢我的家人,他们更多的操劳才使我有时间写这些.
to Google, without its motivation, I have no idea about Ajax, and without it, I hardly complete this code.感谢Google,因其而起而又得其力.
to dong_li98@hotmail.com, we do together an exciting action by submitting a form twice with javascript, which contributes mainly to this script. 感谢dong_li98@hotmail.com,我们一起制造了一个表单递交两次的尝试,这个思想对本文影响很大.
to AjaxAnywhere[JSP based Ajax], to Prototype[javascript extend library], to W3C, to RFC, to DOM, to lots of people who works on js and share their scripts with us … 还要感谢AjaxAnywhere[jsp 环境中的Ajax],Prototype[javascript的扩展库], W3C, RFC, DOM 和其他许多在网上共享他们的js脚本的人…

NOTICE:
DO NOT USE THIS COMMERICALLY WITHOUT AUTHOR’S PAPER AUTHORIZATION.仅供个人免费使用,请勿复制用于商业获利.
if needed, please contact with us at wadelau@ufqi.com 如有必要请联络 wadelau@ufqi.com 获取授权.

From:  http://wadelau.net/gtajax/

此条目发表在转载分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>