Solved

Javascript, containsOnly Chars

Posted on 2014-02-27
2
231 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
ID: 39893169
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
ID: 39893238
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses four methods for overlaying images in a container on a web page
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.…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

792 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