Re: Validation with Javascript
by "Steve Baty" <steve(at)redsquare.com.au>
|
| Date: |
Thu, 30 Nov 2000 16:59:16 +1100 |
| To: |
"Susan Walsh" <susan(at)equilibriumdesign.com>, <hwg-languages(at)hwg.org> |
| References: |
equilibriumdesign |
| |
todo: View
Thread,
Original
|
|
Suzy,
One method would be something like the following:
var addValues = 0;
for (var i=0; i < field.value.length; i++){
var checkVar = field.value.charAt(i);
addValues += parseInt(checkVar);
}
if (addValues == 0){
ok=="no";
}
What this code does is take each of the individual characters and add them
together. So, if the value of the field is 123, then addValue would equel 6.
However, if the user just entered a bunch of zeroes, then addValues would
also be 0 and the function would return false (or set ok = "no).
This should be done in addition to what you're already doing. Another way to
achieve what you're already doing is to have something like the following:
for (var i=0; i<field.value.length;i++){
var checkChar = field.value.charAt(i);
if (checkChar < "0" || checkChar > "9") {
ok = "no";
}
}
..rather than manually set a "temp" variable.
Hope this helps
Steve
---------------
Steve Baty
Technical Director, Red Square
www.redsquare.com
----- Original Message -----
From: "Susan Walsh" <susan(at)equilibriumdesign.com>
To: <hwg-languages(at)hwg.org>
Sent: Thursday, November 30, 2000 3:53 PM
Subject: Validation with Javascript
> Hello,
>
> I am using the following to validate a box in a form. The user must to
> digits numbers only, but any entry like "0" , or "00" is invalid. How do
I
> add that to this script? I know that this should be pretty easy, but I
> can't get it.
>
> function validate(field) {
> var valid = "0123456789"
> var ok = "yes";
> var temp;
> for (var i=0; i<field.value.length; i++) {
> temp = "" + field.value.substring(i, i+1);
> if (valid.indexOf(temp) == "-1") ok = "no";
> }
> if (ok == "no") {
> alert("Invalid entry! Only characters and numbers are accepted!");
> field.focus();
> field.select();
> }
> }
>
> Thanks in advance!
>
> Suzy
>
>
HWG: hwg-languages mailing list archives,
maintained by Webmasters @ IWA
This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.