whatg
11-11-2010, 10:59 AM
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!
<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. :thumbsup:
<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. :thumbsup: