Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am making a form in which when user enters any digits in name field nothing will happen and i know that i have to use onkeypress event in textbox but it is not working why? my code is as :
XML
<html>
<head>
<title>string</title>
<script language=javascript>
{
function f(){

var el=document.f1.t1.value;
var re=/^[A-z]+$/;
if(!re.test(el.value) )
 {
alert("please enter char only");
 }

}

 </script>
</head>
<body>
<form name = f1>
<input type = text onkeypress="return f()" name = t1>
<input type=button value=ok >
</form>
</body>
</html>

also i want to validate a user entering only 97 or 98 at first two positions like if user enters 9898569854 or 9756985698 they are acceptable but if user enters 9565896589 then it alerts but this should happen on button click why my code is not working although it seems to me corect. code is as follows:
C#
function phnoValidation()
        {
           var b;
           b = document.form1.TextBox4.value;
            if (b.charCodeAt(0) == 57 && b.charCodeAt(1) == 56)
             {
               return true;
            }
            else if (b.charCodeAt(0) == 57 && b.charCodeAt(1) == 55)
            {
               alert("this number is acceptable");
                return true;
           }
            else
               alert("this number is not acceptable");
            return false;
      }

Thank u.
Posted

 
Share this answer
 
Its easy:
For Validation of Phone Number that starts with only 97,98.
C#
function phval() {
            var a;
            a = document.getElementById("TextBox5").value;
            if (a.charCodeAt(0) == 57 && (a.charCodeAt(1) == 56 || a.charCodeAt(1) == 55)) {
                alert("valid");
            }
            else {
                alert("invalid");
            }
        }

For Character validation that is user cannot enter numbers in name field. code is in( JSP.)
C#
function isNumberKey(evt)
 {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
         return true;

     return false;
 }

Thank u all. ;)
 
Share this answer
 
Comments
Jayshree Gautam 1-Sep-17 3:08am    
how to validate 12 digit mobile with + sign

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900