在我的表单中使用jQuery验证插件进行验证只允许使用英语,但现在的问题是表单文本字段现在应该只允许英文和日文,所有其他字符都应该省略。
我对英文字符进行了验证,但对日语也是如此。我不知道该怎么做。请帮我一个新手。
我的英语验证码是:
<style>
#nwsltrsnd label.error {
color:red;
}
#nwsltrsnd input.error {
border:1px solid red;
}
</style>
<script>
$(function() {
$.validator.addMethod("accept", function(value, element, param) {
return value.match(new RegExp("." + param + "$"));
});
$('#nwsltrsnd').validate({
rules: {
name: { required: true, accept: "[a-zA-Z]+" },
email: { required: true, email: true }},
messages: {
name: { required: "Name is required",
accept: "Invalid name! either english/japanese allowed" },
email: { required: "Email is required!" }
} }); });
</script>
答案 0 :(得分:2)
您可以将自定义验证规则与正则表达式
一起使用请参阅下文,
Regular Expression for Japanese characters
另外,当我在下面尝试时,它可以正常工作:
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<script src="js/jquery.js"></script>
<script src="js/jquery.validate.js"></script>
<title>Validation Test</title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="validate-form">
<table class="table">
<tr>
<td><input type="text" name="first_name" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Validate" /></td>
</tr>
</table>
</form>
<script>
$.validator.addMethod("languageTest", function(value) {
//regEx = '/[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+[々〆〤]+/u';
regEx = /[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+[々〆〤]+/;
if(!regEx.test(value))
return false;
else if(regEx.test(value))
return true;
}, 'Please enter "anto"!');
$("#validate-form").validate({
rules : {
first_name : {
required : true,
languageTest : true
}
},
messages: {
first_name : {
required : 'Dont Leave it as blank',
languageTest : 'Invalid language'
}
}
});
</script>
<style>
.error{color:red;}
</style>
</body>
</html>