Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to apply a javascript validation on form in which if user number starts with 97,98 its okay but if not than raises error. Also in name textbox if user enters any numbers than also it raise error.
my code is as folloows but its not working:
<script language="javascript"  type="text/javascript" >
function call()
{
var a;
a = document.form1.TextBox1.value;
if (a.indexOf(0-9) != -1)
{
alert("You cannot enter numbers in Name field");
}
}
function numcheck()
{
var b;
b = document.form1.TextBox4.value;
if (b.indexof(97) == -1  || b.indexof(98) == -1)
{
alert("please enter number starting from 97 or 98");
}
}

what can i do to remove the error?Thanks.
Posted
Comments
VICK 14-Jun-13 1:22am    
Do you need solution of your problem in any way or you just want to use JavaScript????
Rambo_Raja 14-Jun-13 1:26am    
only javascript...
Rambo_Raja 14-Jun-13 1:24am    
only javascript...

Refer to link below:

JavaScript :Phone Number validation[^]

You just have to modify lil bit,and you are done.

Number starts with 97/98:

If you want to check weather number starts from 97 or 98 then just check for first two digits,if not what you expected throw alert.

If user enters any numbers than also it raise error:

C#
<asp:TextBox ID="verification" runat="server" Width="150px"></asp:TextBox>


C#
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage=""ControlToValidate="verification" ValidationExpression="^[a-zA-Z]">Enter Characters Only</asp:RegularExpressionValidator>
 
Share this answer
 
v2
C#
function validateNumber() {
    var phoneNumber = document.getElementById("phone");
    var validNumber = "0123456789.-";
    for (i = 0; i < phone.length; i++); {
        if (validNumber.indexOf(phoneNumber.charAt(i)) == -1); {
            alert("You have entered an invalid phone number");
            return false;
        }
    }
    return true;
}
 
Share this answer
 
Comments
Rambo_Raja 14-Jun-13 5:41am    
can i use the following code :
The first one is for Name validation and second code is for phone number validation in which number should only starts with 97,98
function name()
{
var a;
a = document.form1.TextBox1.value;
for (i = 0; i < a.length; i++)
{
if (a.indexof(charCodeAt(i) > 48) != -1 && a.indexof(charCodeAt(i) < 58) != -1)
{
alert("please enter valid name");
}

}
}
function phno()
{
var b;
b = document.form1.TextBox4.value;
if (b.charAt(0) == 57 && b.charAt(1) == 56)
{
alert("This number is acceptable");
}
else if (b.charAt(0) == 57 && b.charAt(1) == 55)
alert("this number is acceptable");

}
Nirav Prabtani 14-Jun-13 6:50am    
use this :)

function Validate()
{
var x = document.form1.txtPhone.value;
var y = document.form1.txtMobile.value;
if(isNaN(x)||x.indexOf(" ")!=-1)
{
alert("Enter numeric value")
return false;
}
if (x.length>8)
{
alert("enter 8 characters");
return false;
}
if (x.charAt(0)!="2")
{
alert("it should start with 2 ");
return false
}

if(isNaN(y)||y.indexOf(" ")!=-1)
{
alert("Enter numeric value")
return false;
}
if (y.length>10)
{
alert("enter 10 characters");
return false;
}
if (y.charAt(0)!="9")
{
alert("it should start with 9 ");
return false
}
}

Permalink Posted 18-Aug-11 0:41am
senthil sennu2.3K

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