PDA

View Full Version : Validation



dips_007
03-22-2004, 03:56 PM
how how do i do simple validation on textboes on a website so that when a button is press to post a form and it is not coorect a message box appears.

in to validate a 2 things
1 A number between 1 and 3 digits
2 A time eg 13.00

sad69
03-22-2004, 06:18 PM
Well it would be nice to see your textboxes, but:


function validateNumber(b) {
var n = b.value*1;
return (n >= 1 && n <= 3);
}

function validateTime(b) {
var t = b.value;
var t_array = t.split(".");

var hour = t_array[0]*1;
var mins = t_array[1]*1;

return(hour >= 0 && hour <= 23 && mins >= 0 && mins <= 59);
}

function validateBoxes() {
if(!validateNumber(numBox)) {
alert("Bad Number");
}

if(!validateTime(timeBox)) {
alert("Bad Time");
}
}

<input type="text" name="numBox" id="numBox"/>
<input type="text" name="timeBox" id="timeBox"/>
<input type="button" onClick="validateBoxes"/>


I think that does what you're looking for. I'm sure you could do something with regular expressions too, but this way makes sense to me.

Hope that helps,
Sadiq.