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!

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.

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;

Archived

This topic is now archived and is closed to further replies.

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