只对大写字母和数字添加验证

Add validation only for capital letter and numbers

本文关键字:添加 验证 数字 大写字母      更新时间:2023-09-26

如果用户在字段中使用了小写字母,那么验证工作就像

aBCD1234

不工作意味着表单不会提交

ABCD1234

现在用户将提交表单。

无需使用JavaScript(尤其是jQuery):

<input type="text" pattern="[A-Z0-9]{8}" />

根据需要调整模式,但这将阻止无效输入的表单提交,即使JavaScript被禁用。它也更容易编程。

MDN Doc上的HTML模式属性:https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern

如何:

<input type="text" onkeyup="this.value = this.value.toUpperCase();" />

submit按钮的onclick事件中添加return isValid();:

<input type="button" onclick="return isValid();" />

isValid函数在@janaspage的回答

中有很好的描述