validation

priya_rajakumari's Avatar, Join Date: Apr 2007
Newbie Member
sir in the below program i have used on keypress function to validate that the text field should only be characters but its not working can u suggest any help in that

HTML Code:
<HTML>
<HEAD>
<TITLE>Login Page</TITLE>
<script language=javascript>
function alpha_rule(){
if(event.keyCode>=97 && event.keyCode<=122)
event.returnValue = true
else if(event.keyCode>=65 && event.keyCode<=90)
event.returnValue = true
else
{
alert("Enter only Alphabets");
event.returnValue = false
}}
function alphanumeric_rule(){
if(event.keyCode>=97 && event.keyCode<=122)
event.returnValue = true
else if(event.keyCode>=65 && event.keyCode<=90)
event.returnValue = true
else if(event.keyCode>=48 && event.keyCode<=57)
event.returnValue = true
else if(event.keyCode==95)
event.returnValue = true
else
event.returnValue = false
}
function numeric_rule(){

if(event.keyCode>=48 && event.keyCode<=57)
event.returnValue = true
else
{
 alert("Enter only Alphabets");
event.returnValue = false
}}
function validate(mailids)
{
if(document.table.fn.value.length==0)
{
alert("Enter the First Name")
document.table.fn.focus()
return false
}
else if(document.table.ln.value.length==0)
{
alert("Enter the Last Name")
document.table. ln.focus()
return false
}
else if(document.table.ph.value.length==0)
{
alert("Enter the phone no")
document.table. ph.focus()
return false
}
else if(document.table.ph.value.length!=10)
{
alert("Enter the phone no correctly")
document.table. ph.focus()
return false
}
else if(document.table.ph.value!="1234567890")
{
alert("Enter the  no only")
document.table. ph.focus()
return false
}
else if(document.table.fax.value.length==0)
{
alert("Enter the fax no")
document.table. fax.focus()
return false
}
else if(document.table.zip.value.length==0)
{
alert("Enter the zip code")
document.table. zip.focus()
return false
}
else if(document.table.mid.value.length==0)
{
alert("Enter the memberid")
document.table.mid.focus()
return false
}
else if(document.table.pwd.value.length==0)
{
alert("Enter the password")
document.table. pwd.focus()
return false
}

else if(document.table.rpwd.value.length==0)
{
alert("Enter the re-password")
document.table. rpwd.focus()
return false
}
else if(document.table.rpwd.value!=document.table.pwd.value)
{
alert("password doesnot match")
document.table. rpwd.focus()
return false
}

else if(document.table.emailid.value.length==0)
{
alert("Enter the Emailid")
document.table.emailid.focus()
return false
}
else if(document.table.emailid.value.length > 0)
{
       var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro'
       ,'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al',
       '.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb',
       '.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs',
       '.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci',
       '.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de',
       '.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et',
       '.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh',
       '.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy',
       '.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io',
       '.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
       '.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk',
       '.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg','.mh','.mk',
       '.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw',
       '.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np',
       '.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm',
       '.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rw','.ru','.sa',
       '.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn',
       '.so','.sr','.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj',
       '.tk','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug',
       '.uk','.um','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu',
       '.ws','.wf','.ye','.yt','.yu','.za','.zm','.zw');
       var mai = mailids;
       var val = true;

       var dot = mai.lastIndexOf(".");
       var ext = mai.substring(dot,mai.length);
       var at = mai.indexOf("@");
       var dom = dot - at;
       var sp = mai.indexOf(" ");

       if(dom >= 4 && at >1 && sp == -1)
       {
               for(var i=0; i<arr.length; i++)
               {
                       if(ext == arr[i])
                       {
                               val = true;
                               break;
                       }
                       else
                       {
                               val = false;
                       }
               }
               if(val == false)
               {
                       alert("Your maild "+mai+" is not correct");
                       return false;
               }
       }
       else 
       {
               alert("Your maild "+mai+" is not correct");
               return false;
       }
        return true;  
}
         
            
 


}

</script>
</HEAD>
<body leftmargin=0 topmargin=0 onload=document.table.fn.focus()>
<form name=table onSubmit="return validate(table.emailid.value)">
<center>
<table cellpadding=2 border=0>
<tr><td width='139' align=right>First Name:<font color=red><sup>*</sup></font></td><td width='281'><input type=text name=fn size=20 onkeypress='alpha_rule()'></td></tr>
<tr><td width='139' align=right>Last Name:<font color=red><sup>*</sup></font></td><td width='281'><input type=text name=ln size=20 onKeyPress='alpha_rule()'></td></tr>

<tr><td width='139' align=right>E-mail address:<font color=red><sup>*</sup></font></td><td width='281'><input type=text name=emailid size=20></td></tr>
<tr><td width='139' align=right>phone:<font color=red><sup>*</sup></font></td><td width='281'><input type=text name=ph size=20 onKeyPress='numeric_rule()'></td></tr>
<tr><td width='139' align=right>Fax:<font color=red><sup>*</sup></font></td><td width='281'><input type=text name=fax size=20 onKeyPress='numeric_rule()'></td></tr>
<tr><td width='139' align=right>Position:</td><td width='281'><input type=text name=po size=20></td></tr>
<tr><td width='139' align=right>Organisation:</td><td width='281'><input type=text name=org size=20></td>
<tr><td width='139' align=right>Address:</td><td width='281'><input type=text name=add size=20></td><td width='281'></td></tr>
<tr><td width='139' align=right>zipcode<font color=red><sup>*</sup></font></td><td width='281'><input type=text name=zip size=20></td></tr>
<tr><td width='139' align=right>Country </td><td width='281'><select name="CboCountry" id="CboCountry" tabindex="16" style="position: static;">
       <option value="Select from List">Select from List</option>
       <option value="Afghanistan">Afghanistan</option>
       <option value="Albania">Albania</option>
       <option value="Algeria">Algeria</option></select></td></tr>
<tr><td width='139' align=right>Memberid<font color=red><sup>*</sup></font>: </td><td width='281'><Input type=text name=mid size=20 onKeyPress='alphanumeric_rule()'></td>
<tr><td width='139' align=right>Password<font color=red><sup>*</sup></font>:</td><td width='281'><input type=password name=pwd size=20 onKeyPress='alphanumeric_rule()' ></td></tr>
<tr><td width='139' align=right>ReEnter-Password<font color=red><sup>*</sup></font>:</td><td width='281'><input type=password name=rpwd size=20 onKeyPress='alphanumeric_rule()'></td></tr>
</table>
<br>
<table cellpadding=4 border=0>
<tr><td align=center><input type=submit name=sreg value=Submit style='width:79'></td></tr>
<tr><td><br><font color=red>*</font><font size=2>fields are compulsary,however we recommand that you fill all the fields</font></td></tr>
</table>
</center>
</form>
</body>
</HTML>

Last edited by shabbir; 24Apr2007 at 16:50.. Reason: HTML code block
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Duplicate of do validation to this form. Thread closed.