Click to See Complete Forum and Search --> : How to check the field?


james_cwy
12-01-2003, 10:02 PM
The code used is to chekc the validity of IP address:
//to check if it is valid
<!--
String.prototype.isIPAddress = function () {
if (! /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(this)) return false;
if (RegExp.$1 > 254 || RegExp.$2 > 254 || RegExp.$3 > 254 || RegExp.$4 > 254)
return false;
if (RegExp.$1 < 1 || RegExp.$2 < 1 || RegExp.$3 < 1 || RegExp.$4 < 1) return false;
return true;
//to check if it is null
<!-- Begin
function checkFields() {
missinginfo = "";
if ((document.form.uip1.value == "") || (document.form.uip2.value == ""))
{
missinginfo += "\n - Proper IP Addresses";
}

if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to enter:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
-----------------------------------------------------------------
<form method="post" action="" name=form onSubmit="return checkFields();">
<tr>
<td align=center>Unused IP 1:</td>
<td align=center><input type="text" name="uip1" onChange="if(!this.value.isIPAddress()) {alert('Invalid IP address.Please enter again.');this.value = '';this.focus()}">
</td>
</tr>
<input type="Submit" name="submit" value="Submit">
<input type="Reset" name="reset" value="Clear"

</form>

For one input box, if it is null, it can check. But if it is invalid it cannot check until I click the submit button and the msg popup will actually cover the error msg for invalid IP.
Say I have two input boxes, the onChange will work for the first box but if I had entered an invalid IP for the second box, it will not check until I click the submit button and the same things happen.
Is there a way to solve this?

Thanks

Gollum
12-02-2003, 02:15 AM
Not sure precisely what you are asking, but perhaps the onblur event might help...

<input
type="text"
name="uip1"
onchange="if(!this.value.isIPAddress()) {alert('Invalid IP address.Please enter again.');this.value = '';this.focus()}"
onblur="if ( (this.value != '') && !this.value.isIPAddress() ) {alert('IP address.Please enter again.');this.value = '';this.focus()}"
>

The onblur happens when the input loses focus and requires the user to enter a valid or empty value. Note - you must include the default value as OK, or you may end up with an infinite cycle of two input fields battling out for focus.