By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

Checking for spaces in input validation.

Markus
Expert 5K+
P: 6,050
Making a basic validation script, i hit a snag.

If user was to input say " "(minus quotes), into the textfield and then submit it the length check i have used would return it as true and therefore execute the rest of the code.

Is there a way to compensate for spaces, as to include them in the length?

Or do you have a different idea?

The code:
Expand|Select|Wrap|Line Numbers
  1. function validateInput()
  2. {
  3.  var username = document.sign_up.username
  4.  var password = document.sign_up.password
  5.  var email    = document.sign_up.email
  6.  var secondary_email = document.sign_up.secondary_email
  7.   if(username.length <= 6 || username.value == ""){ //check for length
  8.     alert("Username must be longer than 6 characters!\n\n- mahcuz")
  9.   }
  10.   else if(username.length >= 14){ //check for length
  11.     alert("Username must be shorter than 14 characters!\n\n- mahcuz")
  12.   }
  13.   else{
  14.   document.sign_up.submit()
  15.   }
  16. }
  17.  
Nov 8 '07 #1
Share this Question
Share on Google+
14 Replies


iam_clint
Expert 100+
P: 1,208
Your question is hard to answer because I don't understand what you want to do... do you want the spaces to be stripped and the spaces removed from the length of the input? so the code halts?
Nov 8 '07 #2

P: 66
Hi,
just to confirm your question,
do you want a validation like the one on this website which i am currently working on: http://www.simplicity-design.co.uk/

if you go to the register section and try to enter a name under 5 characters an error is shown.

if this is the problem will happily provide the script for you..

jason
Nov 8 '07 #3

gits
Expert Mod 5K+
P: 5,390
hi ...

you may use the following adaption for the length check:

Expand|Select|Wrap|Line Numbers
  1. username.replace(/ /g, '').length;
kind regards
Nov 9 '07 #4

Markus
Expert 5K+
P: 6,050
This little thing is bugging me _

I'm trying to validate a form and then submit it through javascript.

But i've hit the folllowing problem:

when i do
Expand|Select|Wrap|Line Numbers
  1. function validateInput()
  2. {
  3.  var username = document.form.username
  4.   if(username.length <= 6){
  5.    alert("Username must be longer than 6 characters!")
  6.   }
  7. }
  8.  
But if i enter a space(and nothing else), it for some reason validates and then submits the form.

Anyone know how i can counter this?
Nov 10 '07 #5

100+
P: 210

Expand|Select|Wrap|Line Numbers
  1. var username = document.form.username
  2.   if(username.length <= 6){
  3.  
You need to read the .value property, and you should always strip any leading or trailing spaces before reading the string, and display that it has been done:

Expand|Select|Wrap|Line Numbers
  1. var username = document.form.username.value.replace(/^\s+|\s+$/g,'');
  2. document.form.username.value=username;
  3. if( username.length <= 6 )
  4.  alert("Username must be longer than 6 characters!")
  5.  
Nov 10 '07 #6

Markus
Expert 5K+
P: 6,050
You need to read the .value property, and you should always strip any leading or trailing spaces before reading the string, and display that it has been done:

Expand|Select|Wrap|Line Numbers
  1. var username = document.form.username.value.replace(/^\s+|\s+$/g,'');
  2. document.form.username.value=username;
  3. if( username.length <= 6 )
  4.  alert("Username must be longer than 6 characters!")
  5.  
Thanks for that!

A question though:
Expand|Select|Wrap|Line Numbers
  1. (/^\s+|\s+$/g,'');
  2.  
Could you explain that?

Thanks :)
Nov 10 '07 #7

gits
Expert Mod 5K+
P: 5,390
hi ...

that is a regular Expression that matches leading and/or trailing spaces of the value you want to check. using it with the replace method the way it was done cuts off the spaces and so trims your value for such spaces ...

kind regards
Nov 10 '07 #8

Markus
Expert 5K+
P: 6,050
hi ...

that is a regular Expression that matches leading and/or trailing spaces of the value you want to check. using it with the replace method the way it was done cuts off the spaces and so trims your value for such spaces ...

kind regards
Thanks :)

but i just changed it to
Expand|Select|Wrap|Line Numbers
  1. (/ /, '')
  2.  
It seems to work, but is there something it's not doing now by me changing it?
Nov 10 '07 #9

100+
P: 210
Thanks :)

but i just changed it to
Expand|Select|Wrap|Line Numbers
  1. (/ /, '')
  2.  
It seems to work, but is there something it's not doing now by me changing it?
Yes - it's only removing the first space that it finds, which is why it wasn't given like that.
Nov 10 '07 #10

P: 66
hey gits...

as promissed this little script should help with your username validation, you may need to talor it a little to your script but the basics should be clear!

Expand|Select|Wrap|Line Numbers
  1.  (javascript)
  2.  /* Username error checking */
  3.       $field = "user";  //Use field name for username
  4.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  5.          $form->setError($field, "* Username not entered");
  6.       }
  7.       else{
  8.          /* Spruce up username, check length */
  9.          $subuser = stripslashes($subuser);
  10.          if(strlen($subuser) < 5){
  11.             $form->setError($field, "* Username below 5 characters");
  12.          }
  13.          else if(strlen($subuser) > 30){
  14.             $form->setError($field, "* Username above 30 characters");
  15.          }
  16.          /* Check if username is not alphanumeric */
  17.          else if(!eregi("^([0-9a-z])+$", $subuser)){
  18.             $form->setError($field, "* Username not alphanumeric");
  19.          }
  20.          /* Check if username is reserved */
  21.          else if(strcasecmp($subuser, GUEST_NAME) == 0){
  22.             $form->setError($field, "* Username reserved word");
  23.          }
  24.          /* Check if username is already in use */
  25.          else if($database->usernameTaken($subuser)){
  26.             $form->setError($field, "* Username already in use");
  27.          }
  28.          /* Check if username is banned */
  29.          else if($database->usernameBanned($subuser)){
  30.             $form->setError($field, "* Username banned");
  31.          }
  32.       }
  33.  
  34.       /* Password error checking */
  35.       $field = "pass";  //Use field name for password
  36.       if(!$subpass){
  37.          $form->setError($field, "* Password not entered");
  38.       }
  39.       else{
  40.          /* Spruce up password and check length*/
  41.          $subpass = stripslashes($subpass);
  42.          if(strlen($subpass) < 4){
  43.             $form->setError($field, "* Password too short");
  44.          }
  45.          /* Check if password is not alphanumeric */
  46.          else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
  47.             $form->setError($field, "* Password not alphanumeric");
  48.          }
  49.          /**
  50.           * Note: I trimmed the password only after I checked the length
  51.           * because if you fill the password field up with spaces
  52.           * it looks like a lot more characters than 4, so it looks
  53.           * kind of stupid to report "password too short".
  54.           */
  55.       }
  56.  
Nov 10 '07 #11

Markus
Expert 5K+
P: 6,050
Thanks for that jasone.

Here is my finished code :)
All written by moi, with the help of (how do you say 'you' in french?)
Expand|Select|Wrap|Line Numbers
  1. /********
  2.     Code written by Mark Skilbeck
  3.     http://mahcuz.com
  4.     5,7"
  5.     black hair
  6.     skinny jeans!
  7. ********/
  8. function validateEmail(email)
  9. {
  10.  email = email;
  11.  AtPos = email.indexOf("@");
  12.  StopPos = email.lastIndexOf(".");
  13.     /*This checks that the '.' and '@' are actually present! The posistion returned by the above variables is an interger and will start     at 0 respective of where they are inside the string. If they are not present, there will be no interger returned. Therefore, -1 will     declare there is no '.' OR '@' */
  14.   if(AtPos == -1 || StopPos == -1){
  15.     return false;
  16.   }
  17.     /*Here we check for the (last) posistion of '.' against the posistion of '@' i.e. if the (last) posistion of '.' comes BEFORE the     '@', the email is invalid! */
  18.   if (StopPos < AtPos) {
  19.     return false;
  20.   }
  21.     /*We now need to check what comes between the '@' and '.' If the space between the two is equal to 1, the email is returned as false.     */
  22.   if (StopPos - AtPos == 1) {
  23.     return false;
  24.   }
  25.     /*Nearly done. '@fooledyou.com' would still be slipping through! We simply check to see if the placement of '@' is 0 (remember 0 is     where numbering starts! NOT 1)*/
  26.   if (AtPos == 0){
  27.     return false;
  28.   }
  29.     /*And finally 'youvebeen@fooled.' will be the last situation (for now) that we must wittle out.
  30.     We compare the posistion of '.' to the length of the email(minus one) to make sure '.' is not at the end! */
  31.   if (StopPos == email.length - 1){
  32.     return false;
  33.   }
  34.     // DONE!
  35. }
  36.  
  37. function username_chars(username)
  38. {
  39.  var username = username;
  40.   username.replace(/[^a-zA-Z0-9]/g,'');
  41.   return username;
  42. }
  43. function validateInput()
  44. {
  45.  var email = document.sign_up.email.value.replace(/ /,'') //email with spaces stripped
  46.   document.sign_up.email.value = email;
  47.  
  48.  var username = document.sign_up.username.value.replace(/[^a-zA-Z0-9_-]/g,'') //username with spaces stripped
  49.   document.sign_up.username.value = username;
  50.  
  51.  var secondary_email = document.sign_up.secondary_email.value.replace(/ /g,''); //sec_email with spaces stripped
  52.   document.sign_up.secondary_email.value = secondary_email;
  53.  
  54.   if(username.length <= 6 || username.length >= 14)
  55.   {
  56.     alert("Username must be 7-14 characters long!\n\n username: '"+username+"', is invalid\n\n- mahcuz");
  57.     username.focus()
  58.   }
  59.   else if(validateEmail(email) === false){ 
  60.  
  61. /*call the validate email function. If the return is FALSE, alert. */
  62.     alert("Please enter a valid email!\n\nemail adress: '"+email+"', not valid\n\n- mahcuz");
  63.     email.focus();
  64.   }
  65.   else if(email == secondary_email){
  66.     alert("The secondary email isn't necessary, but if you're going to use it, don't be a tard and put in the email you've selected as your primary one!!");
  67.     document.sign_up.secondary_email.value = "New email!";
  68.   }
  69.   else{
  70.   document.sign_up.submit()
  71.   }
  72. }
  73. function toEmpty()
  74.  var text1 = document.sign_up.secondary_email
  75.   if(text1.value == "New email!"){
  76.     text1.value = "";
  77.   }
  78. }
  79.  
Nov 10 '07 #12

Markus
Expert 5K+
P: 6,050
Yes - it's only removing the first space that it finds, which is why it wasn't given like that.
Sorry i meant to add a 'g' to the end.
Nov 10 '07 #13

100+
P: 210
Sorry i meant to add a 'g' to the end.
That would remove intermediate spaces also.
Nov 11 '07 #14

acoder
Expert Mod 15k+
P: 16,027
Merged threads on the same topic.
Nov 11 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.