简体   繁体   English

如何使用正则表达式删除空格、特殊字符和前导数字?

[英]How to remove spaces, special characters and leading numbers with regex?

I want to create regex that can be used with jQuery that does below我想创建可以与下面的 jQuery 一起使用的正则表达式

  • No spaces没空间了
  • No special characters没有特殊字符
  • Cannot start with number不能以数字开头

I found most solutions for each point online, but I do not understand how to combine all 3. I will use this to alter a HTML form input when the form is submit.我在网上找到了每个点的大多数解决方案,但我不明白如何将所有 3 个组合起来。我将使用它来在提交表单时更改 HTML 表单输入。

Example someone writes '123 John' then it would change the input to just 'John' before submit.例如有人写“123 John”然后它会在提交之前将输入更改为“John”。 Or '123 John Is Me' becomes 'JohnIsMe' etc.或者“123 John Is Me”变成“JohnIsMe”等等。

Why combine them?为什么要把它们结合起来?

Let's assume a user types in something invalid.让我们假设用户输入了一些无效的内容。 Would it be better to say it's invalid, or would it be better to specifically say "cannot start with a number"?是说它无效更好,还是专门说“不能以数字开头”更好?

Test each restriction separately, and return a helpful error message to the user based on what, specifically, is wrong with their input.分别测试每个限制,并根据用户输入的具体错误向用户返回有用的错误消息。

(Also, don't just change what the user types. That's confusing and bad experience as well. Have the user correct it, by telling them what's wrong.) (另外,不要只是改变用户输入的内容。这也是一种混乱和糟糕的体验。让用户纠正它,告诉他们什么是错的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript正则表达式删除空格,特殊字符和数字 - Javascript Regex to remove any spaces, special characters and numbers 正则表达式删除所有前导和尾随特殊字符? - Regex remove all leading and trailing special characters? 正则表达式删除除数字以外的所有特殊字符? - Regex remove all special characters except numbers? 正则表达式 - 删除特殊字符但保留负数 - Regex - Remove special characters but retain negative numbers 正则表达式接受除数字和特殊字符外的所有字符(空格,带重音符号) - Regex acepting all chars(spaces, accentuated) except numbers and special characters 如何从字符串中删除空格和特殊字符? - How to remove spaces and special characters from string? 如何使用正则表达式匹配数字后跟特殊字符和空格的任意组合 - How to match numbers followed by any combination of special characters and spaces using regex 如何不允许用户使用正则表达式在 html 中仅在单词的第一位给出特殊字符、数字、空格 - how to not allow user to give special characters, numbers , spaces only in first place of word in html using regex 正则表达式删除特殊字符,空格和数字(如果第一个字符) - regex to remove special characters, spaces and number if first character 正则表达式转义空格和特殊字符 - regex escape spaces and special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM