[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Javascript, containsOnly Chars

Posted on 2014-02-27
2
Medium Priority
?
251 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:Allen Pitts
[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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

649 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