JavaScript City

Number Validation

This script allows you to check that a field contains a numeric whole number value. Choose whether to only allow positive numbers or positive numbers plus zero.

Script Code

Grab a copy of the file below and place it in the same directory as the Web page on which you will use the script (if you wish to place the script file in a different directory you will need to make the necessary adjustments to the src attribute of the script tag that calls it).

sval002.js

The code below should be placed between the <body> and </body> tags where you want the phone number field to appear.

Example

A working sample of the script is shown below.

  

Options

You can change the appearance of the input box and the button by adding the phone_number id and sub_ph id to your style sheet and editing as desired.

Be sure to set the url of the form's action attribute in the HTML code to reflect your form handling script on your server:

<form action="form_script_url" onsubmit="return check_the_number();">

You can choose how the viewer should format the number by editing the num_division variable in the javascript code (instructions in code comments):

/* Set whether to allow postive, negative or zero.
   Use the following values to set:
   1 = Allow postive numbers (i.e 1, 15, 534, etc.)
   2 = Allow positive numbers and zero (i.e. 0, 14, 235, 01, 0056 etc. )
*/
var num_division = 1;

Accessibility

A backup to the script via a server-side script that validates the number should suffice (and is highly recommended for security reasons to avoid bad data being passed).


eXTReMe Tracker