我有一个表单,如果其中存在 A-Z、a-z 和 0-9 以外的任何字符,我想禁用提交按钮。
<小时/>我现在拥有的是......
<form name=form action=process.cgi method=POST />
<input type=text name=field1 />
<input type=password name=field2 />
<input type=submit value=submit />
</form>
我试图做到这一点,以便在 field1 和 field2 只有 A-Z、a-z 和 0-9 之前不会提交详细信息
最佳答案
<input type="text" id="name">
<input type="button" id="but" onClick="validate()" Value="Submit">
还有你的js
String.prototype.isValid = function(){return /^\w*$/.test(this);}
function validate() {
if(document.getElementById('name').value.isValid()){
alert('valid') ;
} else{
alert('Invalid') ;
}
}
检查这个 demo
更新为包括表单提交 Updated demo
第二次更新 here
关于javascript - 禁用带有字符白名单的表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12362791/