Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts

Friday, January 22, 2010

Javascript Radio Button validation with function

Function to check a radio button if checked or not
function chkRadio(radio)
{
 var i, flg=0;
 
 for(i=0; i<radio.length; i++)
 {
  if(radio[i].checked)
   flg=1;
 }
 
 return flg;
}

Usage:
if(!chkRadio(document.myForm.group1))
{
 alert("Please select");
 return false;
}
.
.
<form name="myForm" .....
.
<input type="radio" value="V1" name="group1">
<input type="radio" value="V2" name="group1">
<input type="radio" value="V3" name="group1">

Thursday, November 26, 2009

Javascript Date Validation / Comparison

Problem:
User enters two dates, say start date and end date, then we need to alert him if the start date is greater than end date.

Solution:
Assume we are getting the date to the input field using a datepicker.

First we have to convert the string into Date() object using
var dObj = new Date(year, month, date); (also note: the month value range from 0 to 11)
and we can compare the dates.

Example:
var x = document.formName;

//assume the date format is YYYY/MM/DD
var sDate1 = x.start_date.value;
var sDate = new Date(sDate1.split('/')[0], parseInt((sDate1.split('/')[1])-1), sDate1.split('/')[2]);
var eDate1 = x.end_date.value;
var eDate = new Date(eDate1.split('/')[0], parseInt((eDate1.split('/')[1])-1), eDate1.split('/')[2]);
if(eDate >= sDate)
{
    alert("End date is greater than start date; please try again....");
    return false;
}

-

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

Wednesday, June 18, 2008

Radio button validation in Javascript (HTML)

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Check Test</title>
<script language="javascript">
function valBox()
{
var x = document.chkForm;
var i, flg = 0;

for( i=0; i<x.grp.length; i++ )
{
if(x.grp[i].checked)
flg=1;
}
if(!flg)
{
alert("Select One");
return false;
}
}
</script>
</head>
<body>
<form name="chkForm" method="POST" action="#" onsubmit="return valBox();">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td><input type="radio" value="V1" name="grp">1</td>
</tr>
<tr>
<td><input type="radio" value="V2" name="grp">2</td>
</tr>
<tr>
<td><input type="radio" value="V3" name="grp">3</td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Friday, May 16, 2008

Checkbox validation in Javascirpt

Try this:
Change checkbox values and click button.....

<form name="test">
<input type="checkbox" name="checkgroup" checked />
<input type="checkbox" name="checkgroup" />
<input type="checkbox" name="checkgroup" checked />
<input type="button" value="Button" name="B1" onclick="check_boxes();"></form>

<script type="text/javascript">
function check_boxes()
{
for (i=0; i<document.test.checkgroup.length; i++){
if (document.test.checkgroup[i].checked==true)
alert("Checkbox at index "+i+" is checked!")
}
}
</script>


From: http://www.javascriptkit.com/jsref/checkbox.shtml