S L A C K E R Posted August 1, 2009 Share Posted August 1, 2009 Basically, I'd like to allow spaces as well with my registration. Can someone tell me what I need to alter / change to allow this. The current code is: if (document.mainForm.userName.value.length=='') { alert("Please enter a user name."); document.mainForm.userName.focus(); return enable_submit(); } if (!inValidCharSet(document.mainForm.userName.value,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")) { alert("Only letters, numbers, and underscores are allowed in usernames"); document.mainForm.userName.focus(); return enable_submit(); Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 2, 2009 Share Posted August 2, 2009 You are using a custom function called inValidCharSet(). I can't tell you if it is even possible for that function to allow spaces without seeing it. But, what you are asking for can be achieved very easily with a simple regex expression var uName = document.mainForm.userName; var error = false; if (uName.value.length=='') { error = "Please enter a user name."; } else if (uName.value.match(/[^\w ]/g)) { error = "Only letters, numbers, and underscores are allowed in usernames"; } if (error!==false) { uName.focus(); return enable_submit(); } return true; However, since you are allowing spaces you will want to trim the value first to remove leading and trailing spaces. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 2, 2009 Share Posted August 2, 2009 I forgot to add aline to alert the error. Revised: var uName = document.mainForm.userName; var error = false; if (uName.value.length=='') { error = "Please enter a user name."; } else if (uName.value.match(/[^\w ]/g)) { error = "Only letters, numbers, and underscores are allowed in usernames"; } if (error!==false) { alert(error); uName.focus(); return enable_submit(); } return true; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.