Jump to content

Trying to add "Spaces" to allowed characters on Registration Page


S L A C K E R

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.