javascript validation error message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karen987
    New Member
    • Mar 2007
    • 114

    #1

    javascript validation error message

    I keep getting this error message after adding javascript form validation. If anyone goes to the page with the form below, they get the invite to debug

    I have tried adding it to the onSubmit and Onclick but same problem. Any tips? thanks in advance


    Line: 167
    Error Expected '}'



    this is the line causing the problem for some reason,


    Code:
           <input type="Submit" onClick="return ValidateRegister()" value="Register Now" />
                       <br /></td>
    and this is the function

    Code:
     
      function ValidateRegister(){     
        
    if (document.frm.email.value.length >0) {
      i=document.frm.email.value.indexOf("@")
      j=document.frm.email.value.indexOf(".",i)
      k=document.frm.email.value.indexOf(",")
      kk=document.frm.email.value.indexOf(" ")
      jj=document.frm.email.value.lastIndexOf(".")+1
      len=document.frm.email.value.length
    
      if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
      }
      else {
       alert("Please enter a valid email address.\n" +
      document.frm.email.value + " is invalid.");
      document.frm.email.focus()
      return false; }
      
      
      var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
       if(!regExp.test(frm.username.value))
         alert("no special characters")
       if(frm.username.value.length > 18 || frm.username.value.length < 5)
    alert("username should be between 5-18 characters")
     for(i=0;i<names.length;i++) {
     document.frm.username.focus()
       return false; }
      
      
      var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
       if(!regExp.test(frm.password.value))
         alert("no special characters")
       if(frm.password.value.length > 18 || frm.password.value.length < 5)
    alert("password should be between 5-18 characters")
     for(i=0;i<names.length;i++) {
     document.frm.password.focus()
       return false; }
      
      var alphaExp = /^[a-zA-Z]+$/;
     if(document.frm.name.match(alphaExp)){
      return true; }
     else {
      alert("letters only");
     document.frm.name.focus();
      return false; }
      
      
           if(document.frm.name.value.length<5) {  
       alert("minimum 5 characters for name.")
       document.frm.name.focus()
       return false;  }
       
       if(document.frm.name.value.length>30) {  
       alert("maximum 30 characters for name")
      document.frm.name.focus()
    return false;  
       } 
          return true; // 
      
        }
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Have you tried embedding the validation directly into your elements? That makes it a lot easier when it comes time to validate the whole form.

    Join 420,000+ software developers and IT professionals


    The problem may be that you're missing a semicolon at this line:
    Code:
    document.frm.password.focus()
    (occurs a few times)

    As a sidenote, why does a password have to be less than 18 characters long? Aren't you going to hash it anyway?

    Comment

    • RKZA
      New Member
      • Mar 2007
      • 8

      #3
      Hi,
      I had a quick scan through your code and I think for the first if statement you have not closed off with a curly bracket "}".

      R.K.

      Comment

      • karen987
        New Member
        • Mar 2007
        • 114

        #4
        Thanks for replying.

        Do you mean i should close the first part like this(below) or do you mean right at the end of what i posted above?

        As for the password, there is no particular reason why i limited to 18 i just don't want a situation where people type in very long passwords. No other reason. Why is it bad practice?

        Code:
        if (document.frm.email.value.length >0) {
          i=document.frm.email.value.indexOf("@")
          j=document.frm.email.value.indexOf(".",i)
          k=document.frm.email.value.indexOf(",")
          kk=document.frm.email.value.indexOf(" ")
          jj=document.frm.email.value.lastIndexOf(".")+1
          len=document.frm.email.value.length
        
        }

        Please can anyone see what is wrong with line 6 in this code? There is an error in the 6th line.
        Does anyone know what i need to change? im not sure if the 6th line should be
        "for(i=0;i<docu ment.frm.userna me.length;i++) {" instead of what is there now.


        Code:
        var regExp = /^[A-Za-z][A-Za-z0-9_]*$/; 
           if(!regExp.test(frm.username.value)) 
             alert("no special characters or spaces") 
           if(frm.username.value.length > 18 || frm.username.value.length < 5) 
        alert("'Username' should be between 5-18 characters.") 
        for(i=0;i<names.length;i++) { 
        document.frm.username.focus() 
                    return false; }
        thnaks in advance

        Comment

        • RKZA
          New Member
          • Mar 2007
          • 8

          #5
          Yip I think it be best to close the if statement after the variable "len".

          For your line six you don't need the for statement to find set focus on the form field username, just keep the code: document.frm.us ername.focus(). This goes for the same in you passward section.

          Now the only reason i can think of what you were tring to achieve with your for statement is to highlight all the characters in the text field. To do this you don't need a for statement just first document.frm.us ername.focus() then document.frm.us ername.select(); Not sure but i thought i would mention it.

          R.K.

          Comment

          • karen987
            New Member
            • Mar 2007
            • 114

            #6
            Originally posted by RKZA
            Yip I think it be best to close the if statement after the variable "len".

            For your line six you don't need the for statement to find set focus on the form field username, just keep the code: document.frm.us ername.focus(). This goes for the same in you passward section.

            Now the only reason i can think of what you were tring to achieve with your for statement is to highlight all the characters in the text field. To do this you don't need a for statement just first document.frm.us ername.focus() then document.frm.us ername.select(); Not sure but i thought i would mention it.

            R.K.
            RKZA

            actually the email statement works fine the curly bracket screwed it up. If you note, the following code is part of the statement i think. So the email works fine.

            what's giving the problem is the "username" field, the second statement below in bold is the problem. I've tested both individually, and the first one works, but the 2nd one has a problem in that it stops there. ie. after the "username input" has been correctly entered, it should move onto validating whatever is wrong next, but it doesn't. When you click submit the focus goes on "username" but no error message or anything. I know that text entered in "username " is correct and should go through. So obviously the form can't submit, unless it finishes validation.

            I'm sure it's something to do with the regex expression , by the way, i'm not html literate,

            thanks for your help.

            Code:
              if (document.frm.username.value.replace(/ /g, "") == "")  {
                    	alert("Please enter desired username. Must be between 5-18 characters (no special characters).")
                    	document.frm.username.focus() ;
                    return false; }
            
            
            [B]
            var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
               if(!regExp.test(document.frm.username.value))
                 alert("no special characters or spaces in 'username'")
               if(document.frm.username.value.length > 18 || document.frm.username.value.length < 5)
            alert("'Username' should be between 5-18 characters.")
             document.frm.username.focus() ;
            			return false; }[/B]

            by the way, the complete code is below.

            I want to add a statement that makes it mandatory for the user to choose an option from drop down called "country". it's a list of countries obviously. Any help appreciated.

            Thanks a bunch


            Code:
               // VALIDATE ADD
                function ValidateUser(){
            
                    if (document.frm.email.value.replace(/ /g, "") == "")  {
                    	alert("Please enter your email address.")
                    	document.frm.email.focus() ;
                    return false; }	
            		
            		if (document.frm.email.value.length >0) {
            	 i=document.frm.email.value.indexOf("@")
            	 j=document.frm.email.value.indexOf(".",i)
            	 k=document.frm.email.value.indexOf(",")
            	 kk=document.frm.email.value.indexOf(" ")
            	 jj=document.frm.email.value.lastIndexOf(".")+1
            	 len=document.frm.email.value.length	 
             	if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
             	}
             	else {
             		alert("Please enter a valid email address.\n" +
            		document.frm.email.value + " is invalid.");
            		document.frm.email.focus() ;
            		return false; }
            				
                    if (document.frm.username.value.replace(/ /g, "") == "")  {
                    	alert("Please enter desired username. Must be between 5-18 characters (no special characters).")
                    	document.frm.username.focus() ;
                    return false; }
            		
            		var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
               if(!regExp.test(document.frm.username.value))
                 alert("no special characters or spaces in 'username'")
               if(document.frm.username.value.length > 18 || document.frm.username.value.length < 5) {
            alert("'Username' should be between 5-18 characters.")
             document.frm.username.focus() ;
            			return false; }
            				
                    if (document.frm.password.value=="") {
                    	alert("Please enter desired password; must be between 5 and 15 characters.")
                    	document.frm.password.focus() ;
            			return false; }
            			
            				if(document.frm.password.value.length < 5) {  
               alert("Minimum 5 characters for 'password'.")
              document.frm.password.focus() ;
               return false; }
            			
            			if(document.frm.password.value.length > 15) {  
               alert("Maximum 15 characters allowed for 'password'.")
              document.frm.password.focus() ;
               return false; }
                  
                 
                    if (document.frm.PASS2.value=="") {
                    	alert("Please enter password confirmation.")
                    	document.frm.PASS2.focus() ;
                    return false; }	
            						
                    if (document.frm.name.value=="") {
                    	alert("Please enter your name in this format: Lastname, Firstname ")
                    	document.frm.name.focus() ;
                    return false; }	
            		
            		if(document.frm.name.value.length < 5) {  
               alert("Minimum 5 characters for 'name'.")
              document.frm.name.focus() ;
               return false; }
            			
            			if(document.frm.name.value.length > 30) {  
               alert("Maximum 30 characters allowed for 'name'.")
              document.frm.name.focus() ;
               return false; }
            				
            		
            		if (document.frm.city.value.replace(/ /g, "") == "")  {
                    	alert("Please enter your city.")
                    	document.frm.city.focus() ;
                    return false; }
            		
            		if(document.frm.city.value.length < 3) {  
               alert("Minimum 3 characters for 'city'.")
              document.frm.city.focus() ;
               return false; }
            			
            			if(document.frm.city.value.length > 20) {  
               alert("Maximum 20 characters allowed for 'city'.")
              document.frm.city.focus() ;
               return false; }
            		
            		if (document.frm.state.value.replace(/ /g, "") == "")  {
                    	alert("Please enter your state.")
                    	document.frm.state.focus() ;
                    return false; }
            		
            		if(document.frm.state.value.length < 2) {  
               alert("Minimum 2 characters for 'state'.")
              document.frm.state.focus() ;
               return false; }
            			
            			if(document.frm.state.value.length > 20) {  
               alert("Maximum 20 characters allowed for 'state'.")
              document.frm.state.focus() ;
               return false; }		
            	 
               var mycheckbox = document.frm.opt15; 
            if (mycheckbox.checked == false) { 
               alert("this checkbox is mandatory") 
               return false; } 
              return true; 	}
            } //<--this one

            Comment

            • RKZA
              New Member
              • Mar 2007
              • 8

              #7
              Hi,
              Just focusing on the script in bold i can identify a simple error,
              you have have forgot to open your curly bracket, see below,
              if(document.frm .username.value .length > 18 || document.frm.us ername.value.le ngth < 5) { <<<<missing bracket
              [HTML]var regExp = /^[A-Za-z][A-Za-z0-9_]*$/;
              if(!regExp.test (document.frm.u sername.value))
              alert("no special characters or spaces in 'username'")
              if(document.frm .username.value .length > 18 || document.frm.us ername.value.le ngth < 5) {//missing bracket
              alert("'Usernam e' should be between 5-18 characters.")
              document.frm.us ername.focus() ;
              return false; }[/HTML]

              So this would return false all the time without this bracket.

              R.K.

              Comment

              • karen987
                New Member
                • Mar 2007
                • 114

                #8
                OK thanks the bracket did the trick for that one,

                now ive got this problem, with the checkbox, the browser returns a debug error, i don't get the validation script, below, instead the browser says

                error: document.frm.op t15. is null or not an object

                when i debug it points to the below code, i wonder what is wrong with it? the validation does go through, it's just that i don't get the alert, but on the server validation, it shows that it goes through


                Code:
                var mycheckbox = document.frm.opt15; 
                if(mycheckbox.checked == false) { 
                   alert("this checkbox is mandatory") 
                   return false; }
                ive tried,

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  [sidenote post]

                  Regarding password length enforcement, I have never (nor do I ever plan to) require my Users to conform their passwords to any specification (aside from length > 0). Thanks to password managers on every platform, it is now feasible for a User to have his password be rot13([the entire text of War and Peace]). And the database doesn't care b/c it'll just SHA-1 the whole thing anyway. So we only ever need to store 40 characters.

                  Meanwhile, since my password reset/recovery process is completely automatic, it doesn't bother me at all if my Users are forgetting their passwords. It just means that they might want to consider a more memorable password, or else they'll have to keep resetting their passwords.

                  The fact is, the User's own perceptions of the importance of the role that your site plays in his life, as well as his own experiences and prejudices, will determine how secure he makes his password. To force the User to do otherwise really undermines one of the fundamental concepts of the web: the User is supposed to be in control at all times.

                  And aside from all that, the only reason that you would ever NEED to enforce a password length would be if you're storing it in cleartext. And it is for this reason that I am suspicious of "maximum password length".

                  Here's your soapbox back. Thanks for your time.

                  Comment

                  • karen987
                    New Member
                    • Mar 2007
                    • 114

                    #10
                    Originally posted by pbmods
                    [sidenote post]

                    Regarding password length enforcement, I have never (nor do I ever plan to) require my Users to conform their passwords to any specification (aside from length > 0). Thanks to password managers on every platform, it is now feasible for a User to have his password be rot13([the entire text of War and Peace]). And the database doesn't care b/c it'll just SHA-1 the whole thing anyway. So we only ever need to store 40 characters.

                    Meanwhile, since my password reset/recovery process is completely automatic, it doesn't bother me at all if my Users are forgetting their passwords. It just means that they might want to consider a more memorable password, or else they'll have to keep resetting their passwords.

                    The fact is, the User's own perceptions of the importance of the role that your site plays in his life, as well as his own experiences and prejudices, will determine how secure he makes his password. To force the User to do otherwise really undermines one of the fundamental concepts of the web: the User is supposed to be in control at all times.

                    And aside from all that, the only reason that you would ever NEED to enforce a password length would be if you're storing it in cleartext. And it is for this reason that I am suspicious of "maximum password length".

                    Here's your soapbox back. Thanks for your time.
                    pbmods,

                    I read your message, and i'm not sure i understand the part above. What are you suspicious of?

                    I'm limiting the password, simply because the other fields are limited too. I didn't know there was a taboo against it as you're implying. Nor did i understand what you said about password recovery and rots or whatever.

                    For the record, i'm not a programmer or web developer, and know hardly anything about these things, I am not html or ASP literate, and i've gathered together an assortment of validation functions, which i'm trying to gel together. I don't understand the different statements and functions, etc.

                    I suggest you think twice, before you accuse someone of some imaginary crime next time.

                    Thank you for you tips though.

                    All i want is to create validations which are all equal, in length where possible. I was not aware that is a crime.

                    Comment

                    • RKZA
                      New Member
                      • Mar 2007
                      • 8

                      #11
                      Hi dude,
                      So if you want to check that you check box is ticked, you would write

                      [HTML]if(!mycheckbox. checked){ //what ever code }[/HTML]

                      R.K.

                      Comment

                      • lakshmanarayanan
                        New Member
                        • Apr 2007
                        • 5

                        #12
                        I think you are missing the braces in

                        if(!regExp.test (frm.username.v alue))
                        alert("no special characters")
                        if(frm.username .value.length > 18 || frm.username.va lue.length < 5)
                        alert("username should be between 5-18 characters")

                        try with braces.

                        Comment

                        Working...