Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
C#
if (y.charAt(0)!="9"||y.charAt(0)!="8"||y.charAt(0)!="7"||y.charAt(0)!="6")
           {
                alert("it should  with 9 or 8 or 7 or 6");
                theForm.txtPhone.focus();
                return false
           }

i am writing this code for validating phone number
but is working only for number start with 9 only.

i think my or operation is wrong .


my full code is



C#
var y = theForm.txtPhone.value;
       if (theForm.txtPhone.value == "")
       {
       alert("Enter phone number");
              theForm.txtPhone.focus();
              return false;
       }
       if(isNaN(y)||y.indexOf(" ")!=-1)
           {
              alert("Enter numeric value");
              theForm.txtPhone.focus();
              return false;
           }
           if (y.length != 10)
           {
                alert("enter 10 characters");
                theForm.txtPhone.focus();
                return false;
           }
           if (y.charAt(0)!="9"||y.charAt(0)!="8"||y.charAt(0)!="7"||y.charAt(0)!="6")
           {
                alert("it should  with 9 or 8 or 7 or 6");
                theForm.txtPhone.focus();
                return false
           }


please help me

vara prasad
Posted
Updated 22-May-11 23:35pm
v3

Use && instead

if (y.charAt(0)!="9" && y.charAt(0)!="8" && y.charAt(0)!="7" && y.charAt(0)!="6")
 
Share this answer
 
v2
Comments
PRASAD GDV 23-May-11 5:48am    
thank you very much it is working
Prerak Patel 23-May-11 5:54am    
You are welcome.
PRASAD GDV 23-May-11 5:50am    
but how it is possible as we are checking for 'or' condition
Prerak Patel 23-May-11 5:53am    
You can check in 2 ways,
1. Number starts with this or this or this
2. Number doesn't start with this and this and this

So, you can use
1. == with ||
2. != with &&
PRASAD GDV 23-May-11 5:52am    
ok i know the reson
Please check the logic again, I believe you should use the "AND" operator instead of "OR".

OR

Use "==" instead of "!=" if you want to keep "OR" operator.
 
Share this answer
 
Comments
PRASAD GDV 23-May-11 5:52am    
yes you r correct
in this case you need to check with "And" condition instead of "OR"
if you want to use "OR" then you need to check the opposite condition like below

if (y.charAt(0)=="0"||y.charAt(0)!="1"||y.charAt(0)!="2"||y.charAt(0)!="3"||y.charAt(0)!="4"||y.charAt(0)!="5")
 
Share this answer
 

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