Thursday, September 25, 2008

Form validation with javascript

Here is an example for form validation with javascript
<html>
<head>
<title>From Validation</title>
<script language="javascript">
function chkForm(form)
{
if(form.cname.value == ""){ alert("Enter Name"); form.cname.focus(); return false; }
if(form.age.value == "0"){ alert("Select Age Level"); return false; }
}
</script>
</head>
<body>
<div align="center">
<form name="test" action="some_page.php" method="post" onsubmit="return chkForm(this);">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td width="139">Name</td>
<td><input type="text" name="cname" size="20"></td>
</tr>
<tr>
<td width="139">Age</td>
<td><select size="1" name="age">
<option selected value="0">Select</option>
<option value="below 18">below 18</option>
<option value="18+">18+</option>
</select></td>
</tr>
<tr>
<td width="139"> </td>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</form>
</div>
</body>
</html>

Older posts
Radio button validation:
http://phpbeginners.blogspot.com/2008/06/radio-button-validation-in-javascript.html
checkbox validation:
http://phpbeginners.blogspot.com/2008/05/checkbox-validation-in-javascirpt.html

how to change the alert box title?

Want to know how to change the alert box title in javascript...
the answer is: we cant change the title of js alert box.

however, we can use popup boxes, that look like alert boxes with custom title.... but there is a chance - some browsers will block that.
Or we can use vbscript msgBox, but cant get the result in FF.
Or we can use custom designs like this:
http://www.acejs.com/scriptsfolder/121015/121015.html

BUT it is not working like exactly as a alert box. (following codes will get executed though we dont click the ok button)

My opinion: Its better not to use any other custom alert boxes.