英文:
How to modify a regular expression to validate a text box with letters and numbers only, excluding certain characters like !@#$%^&*+=
问题
正则表达式用于文本框验证。我需要验证文本框,条件如下:
- 可以输入数字和字母。
- 需要避免使用以下字符 !@#$%^&*+=
目前我正在使用以下正则表达式,但它不起作用。
<input type="text" pattern="^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$" />
你能帮我找到正确的正则表达式吗?
英文:
Regular Expression for text box validation. I need to validate textbox. conditions like below
- can enter numbers and letters.
- need to avoid these characters !@#$%^&*+=
Currently i am using below regular expression. but it is not working.
<input type="text" pattern="^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$" />
can u help me to find correct regular expression
答案1
得分: 1
以下是您要翻译的内容:
"您目前正在使用的规则表达式用于验证电子邮件地址。
因此,您必须使用正则表达式来验证文本框,使其只包含字母和数字,并避免字符(!@#$%^&amp; * + =)。
^ [a-zA-Z0-9] + $
这将匹配任何只包含字母或数字的字符串。它不会匹配任何包含字符!@#$%^&amp; * + =的字符串。
<!--开始片段:js隐藏:false控制台:true babel:false -->
<!--语言:lang-html -->
<input type =“ text” pattern =“ ^ [a-zA-Z0-9] + $” />
<!--结束片段-->
希望这能帮助到您。谢谢!如果需要更多解释,请参考此链接https://laasyasettyblog.hashnode.dev/validating-username-using-regex。
通过您提出的问题改进我的回答,
<!--开始片段:js隐藏:false控制台:true babel:false -->
<!--语言:lang-html -->
<!DOCTYPE html>
<html>
<head>
<title>测试模式</title>
</head>
<body>
<input type =“ text” id =“ input” pattern =“ ^ [a-zA-Z0-9./|()?~<>\";:;] + $” />
<button onclick =“ validate()”>验证</button>
<script>
function validate() {
var input = document.getElementById(“input”).value;
var pattern = /[a-zA-Z0-9./|()?~<>\";:;]+/;
if (pattern.test(input)) {
alert(“有效”);
} else {
alert(“无效”);
}
}
</script>
</body>
</html>
<!--结束片段-->
英文:
The regulation expression you are currently using is for validate email addresses.
So you have to use a regular expression to validate textboxes, make it only contain letters and numbers and avoid the characters (!@#$%^&*+=).
^[a-zA-Z0-9]+$
This will match any strings that is a letter or a number. It will not match any strings that any characters !@#$%^&*+=.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input type="text" pattern="^[a-zA-Z0-9]+$" />
<!-- end snippet -->
Hope this will help. Thank you!
Refer to this link if you need more clarifications https://laasyasettyblog.hashnode.dev/validating-username-using-regex
Improving my answer with the question u asked,
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<title>Test Pattern</title>
</head>
<body>
<input type="text" id="input" pattern='^[a-zA-Z0-9./|()?~<>":;]+$' />
<button onclick="validate()">Validate</button>
<script>
function validate() {
var input = document.getElementById("input").value;
var pattern = /[a-zA-Z0-9./|()?~<>":;]+/;
if (pattern.test(input)) {
alert("Valid");
} else {
alert("Invalid");
}
}
</script>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论