Solved

Javascript, containsOnly Chars

Posted on 2014-02-27
2
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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 how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
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 …

726 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