我使用jQuery Validate插件进行验证。现在我希望textbox假设只接受国家/地区标题部分中的字母。怎么做?
以下是我的代码:
<form class="form-horizontal tasi-form" method="post" name="register-form" id="register-form" >
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Country Code</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="countrycode" id="countrycode">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Country Title</label>
<div class="col-sm-10">
<input type="text" class="form-control round-input" name="countryname" id="countryname">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Country ISD Code</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="countryisdcode" id="countryisdcode">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 col-sm-2 control-label">Descrition</label>
<div class="col-lg-10">
<textarea name="" class="form-control" id="" cols="30" rows="10"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 col-sm-2 control-label"></label>
<div class="col-lg-10">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script>
(function($,W,D)
{
var JQUERY4U = {};
JQUERY4U.UTIL =
{
setupFormValidation: function()
{
//form validation rules
$("#register-form").validate({
rules: {
countrycode: {
required: true,
minlength:2,
integer:false
},
countryname: "required",
/* countryisdcode:
{ required :true,
integer:true,
minlength: 2
},*/
},
messages: {
countrycode: {
required:"Please enter Countrycode",
minlength:"Minimum length is 2",
integer:"Please enter only "
},
countryname: "Please enter CountryName",
/* countryisdcode: {
required :"Please enter CountryISDcode",
integer:"Please enter Only numbers",
minlength:"minimum length is 2"
}, */
},
submitHandler: function(form) {
form.submit();
}
});
}
}
//when the dom has loaded setup form validation rules
$(D).ready(function($) {
JQUERY4U.UTIL.setupFormValidation();
});
})(jQuery, window, document);
</script>
答案 0 :(得分:0)
您需要使用正则表达式
创建自定义方法$.validator.addMethod("regx", function(value, element, regexpr) {
return regexpr.test(value);
}, "Letters only");
然后:
$("#register-form").validate({
rules: {
countrycode: {
required: true,
minlength: 2,
integer: false
},
countryname: {
required: true ,
regx: /^[a-zA-Z]+$/
}
}
});
如果此版本不符合您的需要,请更改正则表达式。