简体   繁体   中英

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

  • 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.

Example someone writes '123 John' then it would change the input to just 'John' before submit. Or '123 John Is Me' becomes 'JohnIsMe' etc.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM