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! Link to comment https://forums.phpfreaks.com/topic/168435-trying-to-add-spaces-to-allowed-characters-on-registration-page/ 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. Link to comment https://forums.phpfreaks.com/topic/168435-trying-to-add-spaces-to-allowed-characters-on-registration-page/#findComment-888761 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; Link to comment https://forums.phpfreaks.com/topic/168435-trying-to-add-spaces-to-allowed-characters-on-registration-page/#findComment-888956 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.