Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    45
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation Form validation doesn't work in FF

    Hi, doing form validation at college just now. The lecturer gave all of us this following example. But it doesn't seem to work in firefox. I have tried my own scripts and they seem to work so have no idea whats happening!

    Code:
    <html>
    <head>
    <title>Javascript validation program  - limits field length and content</title>
    <script type="text/javascript">
    
    function ValidateForm()
    {
    var msg='';
    if(document.getElementById('CC').value=='')
      {
    	msg+='- Please enter CC\n\n';
      }
    else
    // Now test if CC is purely 2 capital letters...
    
      {
       var CC=RTrim(document.getElementById('CC').value);
    // alert(CC);
       if (CC.length==2)
       {
          if (isCHAR(CC)==false) 
          {
           msg+=' - CC not solely 2 capital letters \n\n';
          }
       }
       else
           msg+=' - CC not 2 letters in length \n\n';
      }
    
    if(document.getElementById('NNNNNN').value=='')
     {
    	msg+='- Please enter NNNNNN \n\n';
      }
    else
      {
    // Now test if NNNNNN is purely 6 digits...
    
       var NIdigits=RTrim(document.getElementById('NNNNNN').value);
       if (NIdigits.length==6)
       {
       if (isInteger(NIdigits)==false) 
       {
           msg+=' - NNNNNN not solely digits \n\n';
       }
    }
    else
       msg+=' - NNNNNN not 6 digits in length\n\n';
       }
       
    // Lastly check if the last field contains only 1 char
       
    if(document.getElementById('C').value=='')
     {
    	msg+='- Please enter C \n\n';
    
     }
    else
    // Now test if C is a single letter...
    
      {
       var C=RTrim(document.getElementById('C').value);
    
      if (C.length==1)
       { 
       if (isCHAR(C)==false)
       {
           msg+=' - C not a capital letter \n\n';
       }
       }
       else
       msg+=' C not 1 character in length';
       }
    // alert(msg);
    if(msg!='')
      {
    //alert('Here...');  
      alert('The following fields are empty and/or invalid:\n\n'+msg);
    	return false
      }
    else
      {
    	return true 
      }
    }
    
    function RTrim(str){
    	if (str==null){return null;}
    	for(var i=str.length-1;str.charAt(i)==" ";i--);
    	return str.substring(0,i+1);
    	}
    	
    //-------------------------------------------------------------------
    // isBlank(value)
    //   Returns true if value only contains spaces
    //-------------------------------------------------------------------
    function isBlank(val){
    	if(val==null){return true;}
    	for(var i=0;i<val.length;i++) {
    		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
    		}
    	return true;
    	}
    
    //-------------------------------------------------------------------
    // isInteger(value)
    //   Returns true if value contains all digits
    //-------------------------------------------------------------------
    function isInteger(val){
    	if (isBlank(val)){return false;}
    	for(var i=0;i<val.length;i++){
    		if(!isDigit(val.charAt(i))){return false;}
    		}
    	return true;
    	}
    //-------------------------------------------------------------------
    // isCHAR(value)
    //   Returns true if value contains all CHARS
    //-------------------------------------------------------------------
    function isCHAR(val){
    	if (isBlank(val)){return false;}
    	for(var i=0;i<val.length;i++){
    		if(!isCAPlet(val.charAt(i))){return false;}
    		}
    	return true;
    	}
    //-------------------------------------------------------------------
    // isDigit(value)
    //   Returns true if value is a 1-character digit
    //-------------------------------------------------------------------
    function isDigit(num) {
    	if (num.length>1){return false;}
    	var string="1234567890";
    	if (string.indexOf(num)!=-1){return true;}
    	return false;
    	}
    
    //-------------------------------------------------------------------
    // isCAPlet(value)
    //   Returns true if value is a 1-character letter
    //-------------------------------------------------------------------
    function isCAPlet(num) {
    	if (num.length>1){return false;}
    	var string="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	if (string.indexOf(num)!=-1){return true;}
    	return false;
    	}
    </script>
    
    </head>
    
    <body bgcolor="#FFFFFF">
    
    <form action='http://10.205.3.202/Valid.php' method='POST'  onsubmit='return ValidateForm();'>
    
    
    
    <H1>Validate NI No</H1>
    
    
    <p>
    
    NI-No    :
    
    <INPUT TYPE="text" id='CC' NAME="CC" SIZE='2' maxlength='2' >
    -
    
    
    <INPUT TYPE="text" id='"NNNNNN"' NAME="NNNNNN" SIZE='6' maxlength='6' >
    
    <INPUT TYPE="text" id='C' NAME="C" SIZE='1' maxlength='1'>
    
    <p>
    
    <INPUT  TYPE="submit" id='Process' value="Process" name="submit">
    
    
    
    </form>
    
    </body>
    </html>

    Any help would be great.
    Last edited by whatg; 11-11-2010 at 11:07 AM. Reason: wrong code

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,519
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
     id='"NNNNNN"'
    should be

    Code:
     id='NNNNNN'
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You (or your teachers) seem to be using double and single quotes (and capitals and lower-case) randomly. E.g.
    <INPUT TYPE="text" id='NNNNNN' NAME="NNNNNN" SIZE='6' maxlength='6' >
    That is likely to give rise to problems. Define a consistent policy and stick to it. var x = "" (two double quotes) is to be preferred to var x = '' (two single quotes), as '' can be mistaken for "

    You could greatly simplify your code by using regular expressions in place of the long-winded and long-obsolete functions isBlank(), isChar(), isDigit() etc. What a pity that teachers are still serving up this old stuff. Is there any other faculty in your college where the syllabus taught is 10 years out of date?


    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •