Javascript syntax issue

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;

 }

Answers


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.


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

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.


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.


Need Your Help

modifying variables in a running swf from an SWFLoader

flex swfloader

I have a flex application that's compiled with flex 4.1.