基于jQuery的表单校验插件
<script type="text/javascript">
$.checkFormSetup({e: 'Please input the text.'});
$("#form").checkForm({
'#username': {w: 'Input the wrong username.', r: /^[a-zA-Z]\w{4,15}$/},
'#email': {e: 'Please input the email.', w: 'Input the wrong email.', r: 'email', t: 'next'},
'input:radio': {e: 'Please select the sex.', t: '#sexTips', type: 'radio'},
'input:checkbox': {e: 'Please select the hobbies.', w: 'Select the wrong hobbies.', r: /^1$/, t: '#hobbiesTips', type: 'checkbox'},
'select': {e: 'Please select the profession.', type: 'select'},
});
</script>
插件内置了一些常用的正则表达式。
int
: 整数正则表达式date
: 日期正则表达式(格式:2013-03-22)datetime
: 时间正则表达式(格式:2013-03-22 16:22)idcard
: 身份证正则表达式phone
: 手机正则表达式email
: 邮箱正则表达式url
: 链接正则表达式qq
: QQ正则表达式chinese
: 中文正则表达式
可用通过设置全局属性来每个校验设置默认值。
$.checkFormSetup({e: 'Please input the text.'});
属性如下:
e
: 输入值为空提示语w
: 输入值错误提示语r
: 输入值校验正则表达式t
: 提示节点选择符(可写next
,prev
获取输入元素的相邻节点或者通过$(seletor)获取节点,默认:next
)type
: 校验通过时提示节点的类名(可选text
,select
,radio
,checkbox
,默认:text
)cpass
: 校验通过时提示节点的类名(默认:check-pass
)cerror
: 校验失败时提示节点的类名(默认:check-error
)