原博客地址:http://www.tuicool.com/articles/y6vqMj
最近業務用到input校驗,網上找到一個比較全面的:
jsp:
<input validtype="QQ" type="text" name="leaguerName" class="easyui-validatebox" style="width: 80%"
data-options="required:true, missingMessage:'QQ不能爲空.'" />
js:<script type="text/javascript">
$.extend($.fn.validatebox.defaults.rules, {
CHS: {
validator: function (value, param) {
return /^[\u0391-\uFFE5]+$/.test(value);
},
message: '請輸入漢字'
},
english : {// 驗證英語
validator : function(value) {
return /^[A-Za-z]+$/i.test(value);
},
message : '請輸入英文'
},
ip : {// 驗證IP地址
validator : function(value) {
return /\d+\.\d+\.\d+\.\d+/.test(value);
},
message : 'IP地址格式不正確'
},
ZIP: {
validator: function (value, param) {
return /^[0-9]\d{5}$/.test(value);
},
message: '郵政編碼不存在'
},
QQ: {
validator: function (value, param) {
return /^[1-9]\d{4,10}$/.test(value);
},
message: 'QQ號碼不正確'
},
mobile: {
validator: function (value, param) {
return /^(?:13\d|15\d|18\d)-?\d{5}(\d{3}|\*{3})$/.test(value);
},
message: '手機號碼不正確'
},
tel:{
validator:function(value,param){
return /^(\d{3}-|\d{4}-)?(\d{8}|\d{7})?(-\d{1,6})?$/.test(value);
},
message:'電話號碼不正確'
},
mobileAndTel: {
validator: function (value, param) {
return /(^([0\+]\d{2,3})\d{3,4}\-\d{3,8}$)|(^([0\+]\d{2,3})\d{3,4}\d{3,8}$)|(^([0\+]\d{2,3}){0,1}13\d{9}$)|(^\d{3,4}\d{3,8}$)|(^\d{3,4}\-\d{3,8}$)/.test(value);
},
message: '請正確輸入電話號碼'
},
number: {
validator: function (value, param) {
return /^[0-9]+.?[0-9]*$/.test(value);
},
message: '請輸入數字'
},
money:{
validator: function (value, param) {
return (/^(([1-9]\d*)|\d)(\.\d{1,2})?$/).test(value);
},
message:'請輸入正確的金額'
},
mone:{
validator: function (value, param) {
return (/^(([1-9]\d*)|\d)(\.\d{1,2})?$/).test(value);
},
message:'請輸入整數或小數'
},
integer:{
validator:function(value,param){
return /^[+]?[1-9]\d*$/.test(value);
},
message: '請輸入最小爲1的整數'
},
integ:{
validator:function(value,param){
return /^[+]?[0-9]\d*$/.test(value);
},
message: '請輸入整數'
},
range:{
validator:function(value,param){
if(/^[1-9]\d*$/.test(value)){
return value >= param[0] && value <= param[1]
}else{
return false;
}
},
message:'輸入的數字在{0}到{1}之間'
},
minLength:{
validator:function(value,param){
return value.length >=param[0]
},
message:'至少輸入{0}個字'
},
maxLength:{
validator:function(value,param){
return value.length<=param[0]
},
message:'最多{0}個字'
},
//select即選擇框的驗證
selectValid:{
validator:function(value,param){
if(value == param[0]){
return false;
}else{
return true ;
}
},
message:'請選擇'
},
idCode:{
validator:function(value,param){
return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value);
},
message: '請輸入正確的身份證號'
},
loginName: {
validator: function (value, param) {
return /^[a-zA-Z0-9_]+|[\u4e00-\u9fa5]+$/.test(value);
},
message: '登錄名稱只允許漢字、英文字母、數字及下劃線。'
},
equalTo: {
validator: function (value, param) {
return value == $(param[0]).val();
},
message: '兩次輸入的字符不一至'
},
englishOrNum : {// 只能輸入英文和數字
validator : function(value) {
return /^[a-zA-Z0-9_ ]{1,}$/.test(value);
},
message : '請輸入英文、數字、下劃線或者空格'
},
xiaoshu:{
validator : function(value){
return /^(([1-9]+)|([0-9]+\.[0-9]{1,2}))$/.test(value);
},
message : '最多保留兩位小數!'
},
ddPrice:{
validator:function(value,param){
if(/^[1-9]\d*$/.test(value)){
return value >= param[0] && value <= param[1];
}else{
return false;
}
},
message:'請輸入1到100之間正整數'
},
jretailUpperLimit:{
validator:function(value,param){
if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){
return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);
}else{
return false;
}
},
message:'請輸入0到100之間的最多倆位小數的數字'
},
rateCheck:{
validator:function(value,param){
if(/^[0-9]+([.]{1}[0-9]{1,2})?$/.test(value)){
return parseFloat(value) > parseFloat(param[0]) && parseFloat(value) <= parseFloat(param[1]);
}else{
return false;
}
},
message:'請輸入0到1000之間的最多倆位小數的數字'
}
});
$(function () {
//設置text需要驗證
$('input[type=text]').validatebox();
})
</script>
引用:
在input 里加入屬性 : validtype ="你要校驗的對應方法", 比如 我要校驗qq,那就是 validtype="QQ".