我想在jquery中进行以下任何插件的验证:1.
用户ID不能包含空格,2.
密码必须大于六个字符,3.
所有字段都是必填字段。
我无法执行此操作,请帮助我。
$('#form').submit(function (event) {
//getting elements
var id=$('#userId').val();
var pw=$('#pw').val();
//validating
//$('span').text('These Fields Are Required...').show().fadeOut(100);
if ( id !== "" && pw !=="") {
var json={'User Id':id,'Password':pw};
$.each(json,function(p,v){
alert(p+": "+v);
});
return;
}
else if( id.indexOf(' ')>=0 ){
$( "span" ).text( "User Id Cannot Contain Space!" ).show().fadeOut( 1000 );
}
if(id==="" || pw==="")
{
$( "span" ).text( "Some Feilds are Required!" ).show().fadeOut( 1000 );
}
event.preventDefault();
});
$('#form').submit(function (e) {
e.preventDefault();
//getting elements
var id = $('#userId').val();
var pw = $('#pw').val();
//validating
try {
//all Fields are Required.
$(".required").each(function () {
if ($(this).val().length == 0) {
$(this).focus();
throw "Some Feilds are Required!";
}
});
//User Id Cannot Contains Space
var userid_ppt = /^\w+$/g;
if( !userid_ppt.test(id) )
throw "User Id Cannot Contains Space";
//Password Must Be greater than Six characters
if( pw.length < 6 )
throw "Password Must Be greater than Six characters";
} catch (emsg) {
$("#msg").html(emsg).stop().show().fadeOut(1000);;
return false;
}
return true;
});
试试吧
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句