Thursday, September 25, 2008

Form validation with javascript

Here is an example for form validation with javascript
<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; }
<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">
<td width="139">Name</td>
<td><input type="text" name="cname" size="20"></td>
<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>
<td width="139"> </td>
<td><input type="submit" value="Submit" name="B1"></td>

Older posts
Radio button validation:
checkbox validation:

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:

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.