Click to See Complete Forum and Search --> : charecter check


mast3r
09-03-2003, 09:03 AM
i have this script for one of my forms. What i want to do is in fields 1,2 and 9 i want to make sure only numbers are placed in the fields. The same for field 3 but allowing "." to be placed in abviously.

Thanks guys/gals


<script language=j"avascript">
function checkForm(form)
{
var aChk =
[
[form.Telephone_Number_of_ADSL_line, "Field 1"],
[form.CBUK_Number, "Field 2"],
[form.IP_attempting_to_use, "Field 3"],
[form.Has_ADSL_ever_worked, "Field 4"],
[form.Are_all_filters_in, "Field 5"],
[form.Does_phone_work_on_ADSL_line, "Field 6"],
[form.Filters_or_SSFP, "Field 7"],
[form.Customer_Contact, "Field 8"],
[form.Customer_Tel, "Field 9"],
[form.Equipment, "Field 10"],
[form.youremail, "Field 12"]
];

var aMsg = new Array;
for ( var i = 0; i < aChk.length; i++ )
{
if ( aChk[i][0].value == "" ) aMsg.push(aChk[i][1]);
}

if ( aMsg.length > 0 )
{
alert(aMsg.join(', ') + " are Missing. Please go back and fill in the missing information. Thankyou");
return false;
}

}
</script>

Khalid Ali
09-03-2003, 11:13 AM
you can validate a string for a number using 2 of these approaches

if(isNaN(Number(string)){
//not a number
}else{
//a number
}


you can replace Number
with parseInt
as well

Xin
09-03-2003, 02:04 PM
parseInt() would mistake number strings like "08" as 0 (the leading 0 has it think octal), better to use parseFloat() and if you just want the interger use Math.floor(parseFloat())

if you want to try some regular expression, you can use:

if (/^\d+$/.test(field.value)) {
// field is all numbers
}
else if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(field.value)) {
// field is good IP address
}

see http://www.yxscripts.com/fg/form.html for some more form checking functions.