-1

Here is my jsFiddle

Its on the Phone method, no the name one

Now is this line right? I only want it to be true if the first 3 letters are 087

var RightStarting3 = value.substring(0,2) == (087);


 if (BlankPass || LessThan10 ||  RightStarting3 || GreaterThan10 || (HasSpaces > 0)) 
 {

    document.getElementById('Phone').style.background = "red";
    return false;
 } 
    else {
    document.getElementById('Phone').style.background = "white";
    document.getElementById("PhoneTick").style.visibility="visible";
    return true;

 }
2
  • 087 is not a string but the number which is equivalent to 87. If you want a string, use a string literal like "087". Commented May 2, 2013 at 20:27
  • @Gumbo, while it's certainly good to change the comparison to === '087', loosely comparing a string to 087 should generally provide the correct results. OP narrowly misses running into issues with octal numbers (077 is the number 63 in decimal, which is misleading to beginners). Commented May 2, 2013 at 20:30

4 Answers 4

3
var RightStarting3 = value.substring(0,3) === ('087');
Sign up to request clarification or add additional context in comments.

1 Comment

Hm can you explain why '000000000' works aswell as '0878888888' ? jsfiddle.net/UTtxA/27
1

value.substring(x) returns a string and 087 and 87 mean the same to javascript interpreter. You should change one of the datatypes so that they match...

Either the substring to an integer:

var RightStarting3 = parseInt(value.substring(0,2)) == 87;

Or the value you're comparing against to a string:

var RightStarting3 = value.substring(0,3) == "087";

Secondly -- you are invoking ValidateName() immediately (in your assignment to NamePass). Is this really necessary? There will be empty values on page load.

2 Comments

can you tell me why the first '086' will work but the other wont? var RightStarting3 = value.substring(0,3) == ('086' || '087');
('086' || '087') will always return '086' -- it gets instantly short-circuited by the javascript interpreter because the static '086' value will never ever be false. If you're trying to test either value, you need to write: var RightStarting3 = value.substring(0,3) == '086' || value.substring(0,3) == '087' Alternatively, you could store the string representation '086' or '087' in a variable, and test it once.
1

I think with the javascript substring(x,y) method, the y value is the value at which to stop the selection. So in your example the first 3 characters will not be selected and instead the first 2 characters will be selected.

var a = "123";

// this returns "12"
alert(a.substring(0,2));

You probably want to use var RightStarting3 = value.substring(0,3) == ('087'); instead.

Comments

0

KingKongFrom's answer is correct, I would add that you should make sure that value (whatever that is) isn't null first, cause if you try to call substring on null it will thrown an exception and die.

1 Comment

can you tell me why the first '086' will work but the other wont? var RightStarting3 = value.substring(0,3) == ('086' || '087');

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.