Solved

Javascript, containsOnly Chars

Posted on 2014-02-27
2
227 Views
Last Modified: 2014-02-28
Good morning expert,

I found this JavaScript in an application called ServiceNow. It is being called,
with a bunch of other validators in an include file.

I get most of it but towards the end it gets a bit fuzzy. The script is copied herewith below.  The following are are statements made about the script. If you see anything that is not true or needs amplification (I am missing an important idea) please offer an amendment.

It is a function which has two parameters validChars and sText.
'var IsNumber' is set to true probably so that every time the function is called it begins with the same value.
After declaring undefined type a var 'c' it does a for for loop.
As is traditional, the for loop begins the i var at zero and says
how long to keep doing the for loop: while i is greater than the the length of the text
AND IsNumber is true.
After 'how long to keep doing the for loop' the value of i is increased by one.
Then our friend 'c' is made to be whatever character is
found in the 'ith' postion.
The first time thru the loop c would then be equal to the the first character
passed to the function by the argument 'sText'
If the function was called as containsOnlyChars(("abcdefghijklmnopqrst"), JavaScript) in the first loop 'c' would be equal to 'J'.
Then a comparitor.    
If 'c' (in the first case the letter J) is not found as one of the characters contained in the list
of characters called validChars then isNumber is false.
The indexOf method is a that looks through a string returns the position of the subject
string, in this case 'c representing a capital J'. If indexOf does not find the sought for string  it returns a -1. So if 'J' is not found  IsNumber
The function then repeats until i is incremented higher than the length of the string or
iSNumber becomes false. So when ever the loop var i gets higher than 9 (for JavaScript) or does not find the character for which it seeks then the loop ends, the function stops and and IsNumber is returned.
The function is probably called with acceptable characters as the first argument
validChars, and the text to be validated as the second argument sText. The use
could be to take some input from the user in a textbox and make it sText and make
sure that the input does not have any invalid characters.

Is this correct? Did I miss something?

Thanks.

Allen in Dallas






++++++++++++beginning of script+++++++++++++++++++
function containsOnlyChars(validChars, sText) {
    var IsNumber=true;
    var c;
 
    for (var i = 0; i < sText.length && IsNumber == true; i++) {
        c = sText.charAt(i);
        if (validChars.indexOf(c) == -1) {
            IsNumber = false;
        }
    }
   
    return IsNumber;  
}
++++++++++++end of script+++++++++++++++++++
0
Comment
Question by:9apit
  • 2
2 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
You have it right however if the only purpose of the function is to determine if any of the characters in the supplied string are not amongst the list of valid characters then you might as well stop looking as soon as the first invalid character is found. Insert a break; to break out of the loop as soon as IsNumber = false;

for (var i = 0; i < sText.length && IsNumber == true; i++) {
        c = sText.charAt(i);
        if (validChars.indexOf(c) == -1) {
            IsNumber = false;
            break;
        }
    }
0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Judging by the name of the var IsNumber, I would say the original purpose of the function would be to determine if there were any numbers in the supplied string. A typical function call might look like this:

var myBooleanVar = containsOnlyChars(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.?!&@()"), "Check this string for any numbers");

The above would return true.

This false:

var myBooleanVar = containsOnlyChars(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.?!&@()"), "Check if 456 is a number");
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now