I am making a registration form. When a user is selecting a username I want jquery to replace anything other than a-z,A-Z,0-9
or a space with nothing.
So on keyup 开发者_如何学Pythonthe form field with the id of username will only allow a-z,A-Z,0-9
or a space and replace anything else with nothing "".
How could I do that?
You shouldn't just replace those characters, as then the user who signed up with me!
will wonder why they can't use that to login. If they have characters they type that also disappear, they will be confused. You will always need to bind other events such as paste
.
Instead, you should validate it with the regular expression replace(/[^a-zA-Z\d ]/g, '')
and then return to the user if it is valid or not.
function myCallback(username) {
username = username.replace(/[^a-z0-9 ]/gi, '');
/* do stuff with username now */
}
.replace(/[^a-z0-9\s]/gi, '').split(' ').join('');
Find space to join
.split(' ').join('');
精彩评论