Verifying user input in JavaScript

By: Dave Reed Emailed: 1678 times Printed: 2154 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This javascript library contains functions for verifying the contents of a text box. For each function the name of a text box is passed as input, along with any restrictions on the contents. If the contents of the box are not legal, a warning is displayed.
function verifyNum(textBox)
    // Assumes: textBox is a text box in the page
    // Results: warns if box contents is not a number
    {
        var boxValue;

        boxValue = parseFloat(textBox.value);
        if ( isNaN(boxValue) ) {
            alert("You must enter a number value!");
        }
    }

    function verifyNumInRange(textBox, low, high)
    // Assumes: textBox is a text box in the page, low <= high
    // Results: warns if box contents is not a number in range low..high
    {
        var boxValue;

        boxValue = parseFloat(textBox.value);
        if ( isNaN(boxValue) || (boxValue < low) || (boxValue > high)) {
            alert("You must enter a number value in the range [" +
                  low + ".." + high + "]");
        }
    }

    function verifyInt(textBox)
    // Assumes: textBox is a text box in the page
    // Results: warns if box contents is not an integer
    {
        var boxValue;

        boxValue = parseFloat(textBox.value);
        if ( isNaN(boxValue) || (boxValue % 1 != 0) ) {
            alert("You must enter an integer value!");
        }
    }

    function verifyIntInRange(textBox, low, high)
    // Assumes: textBox is a text box in the page, low <= high
    // Results: warns if box contents is not an integer in range low..high
    {
        var boxValue;

        boxValue = parseFloat(textBox.value);
        if ( isNaN(boxValue) || (boxValue % 1 != 0) ||
                                (boxValue < low)    || 
                                (boxValue > high)) {
            alert("You must enter an integer value in the range [" +
                  low + ".." + high + "]");
        }
    }

    function verifyOneOf(textBox, options)
    // Assumes: textBox is a text box in the page, options is a list
    // Results: warns if box contents is not one of the options
    {
        for (i = 0; i < options.length; i++) {
            if (textBox.value == options[i]) {
                return;
            }
        }
        
        alert("You must enter one of the following: " + options);
    }
  

Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dave Reed
Pause execution in JavaScript
Verifying user input in JavaScript

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
History and evolution of Javascript
Typecasting in Javascript
Scrolling message on the status bar using Javascript
Getting Browser's height and width using Javascript
Verifying user input in JavaScript
Click to remove an element from a select html form using javascript
Javascript Basics
List of Keywords and reverved words in Javascript
Using toString() in JavaScript to convert data types to String
Bitwise NOT, AND, OR, XOR operators in Javascript
Using ! and !! in Javascript
Using && in Javascript
The if statement in Javascript
Most Emailed Articles (in Javascript)
List of Keywords and reverved words in Javascript
window.frames[i] in Javascript
Scrolling message on the status bar using Javascript
Verifying user input in JavaScript
Typecasting in Javascript
The if statement in Javascript
call() and apply() methods in Javascript
Form validation using Javascript
Pause execution in JavaScript
Javascript Basics
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
do-while, while, for and for-in loops in Javascript
this keyword sample in Javascript
Password protect a web page using Javascript
Javascript to display client date and time on webpage