使用Javascript:
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
} else {
alert(helperMsg);
elem.focus();
return false;
}
}
function isGoodPhrase(elem,helperMsg){
var badPhrase=/EPW|ESW|\s/;
if (elem.value.match(badPhrase)){
alert(helperMsg);
elem.focus();
return false;
} else {
return true;
}
}
function checkInput(id) {
return isAlphanumeric(document.getElementById(id),'Your Submission Contained Invalid Characters')
&& isGoodPhrase(document.getElementById(id), 'Please Enter A Correct Friend Code!');
}
</script>
和HTML
<form action="" method="post">
<div align="center"><legend>Add a Code</legend>
<label for="code"></label>
<input type="text" name="code" id="code" maxlength="10" />
<input type='button' onclick="return checkInput('code');" value='Check Field' />
</form>
</div>
答案 0 :(得分:0)
警报正常,我测试了它们。
使用与正则表达式不匹配的正确值检测您正在测试的问题以查看警报