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

Using jQuery to gather data from HTML attributes

javascript jquery

I'm trying to put together a web form to mark an indeterminate number of employees as either present or absent. The page itself contains an arbitrary number of divs of the form:

jqgrid setColProp not working

javascript jquery jqgrid

I am using setColProp to dynamically load values into a select edittype.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.