PDA

View Full Version : JavaScript Validation



sysout
11-13-2009, 03:03 PM
Js :


<script type="text/javascript" language="javascript">
success = false;
function notNull(id, requirement, errornya){
inputId = document.getElementById(id).value;
err = document.getElementById(errornya);
if (inputId == ""){
err.innerHTML = "* "+requirement + " must be filled";
success = false;
}
else{
err.innerHTML = "";
success = true;
}
}
function mustNumber(id, requirement, errornya){
inputId = document.getElementById(id).value;
err = document.getElementById(errornya);
if (isNaN(inputId) == true){
err.innerHTML = "* "+requirement + " must be a number";
success = false;
}
else{
err.innerHTML = "";
success = true;
}
}
function atLast(form){
if(success == false){
alert("Sorry, still error");
form.gatewayId.focus();
return false;
}
}
</script>


the Form :


<form id="form" action="insertGateway.php" method="post" onsubmit="return atLast(this)">
Gateway Id : <input name="gatewayId" id="gatewayId" type="text" onBlur="notNull('gatewayId', 'Gateway Id', 'errorGateway')" /> <span class="ErrorCss" id="errorGateway"></span><br/>
Com Port : <input name="comPort" id="comPort" type="text" onBlur="notNull('comPort', 'Com Port', 'errorComPort')" /> <span class="ErrorCss" id="errorComPort"></span><br/>
Baud Rate : <input name="baudRate" id="baudRate" type="text" onBlur="notNull('baudRate', 'Baud Rate', 'errorBaudRate'); mustNumber('baudRate', 'Baud Rate', 'errorBaudRate')" /> <span class="ErrorCss" id="errorBaudRate"></span><br/>
</form>


still gives me error..
why it don't want to focus?
the baud rate validation is error too..oh god..fixed it all night, but still errors..
please help me.

thanks