Solved

Javascript, containsOnly Chars

Posted on 2014-02-27
2
230 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

770 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