Page 1 of 1

validating fields in a text box - how?

Posted: Mon May 18, 2009 5:09 am
by rrn
hi all ,

in my website there is a page for entering details..

there is a 3 text boxes for entering date of birth

Code: Select all

<td >Date of Birth</td>
    <td ><input type="text" id="date" name="date" value="" class="box-200" /></td> 
<td ><input type="text" id="month" name="month" value="" class="box-200" /></td> 
<td ><input type="text" id="year" name="year" value="" class="box-200" /></td>
 
i have given a code for checking the values entered in the text

Code: Select all

function validate() {
if(document.form.date.value=="")
{
alert ('Please enter date');
return false;
}
if(document.form.month.value=="")
{
alert ('Please enter month.');
return false;
}
if(document.form.year.value=="")
{
alert ('Please enter year.');
return false;
}
}
 
if the text box is left empty it will display an alert message..

but what i need is
date should be a value between 1 and 31. if the value entered is greater than 31, it should display an alert message
likewise month should be between 1 and 12 and year less than 2009..

what change should i make in the code??
please help me to solve this...

Re: validating fields in a text box - how?

Posted: Mon May 18, 2009 5:32 am
by jayshields
Here's the month one, I'll let you figure out the other two by yourself.

Code: Select all

if(document.form.month.value=="" || document.form.month.value < 1 || document.form.month.value > 12)
{
  alert ('Please enter month.');
  return false;
}

Re: validating fields in a text box - how?

Posted: Tue May 19, 2009 6:34 am
by rrn
Thanks a lot.... it worked....

i have got one more doubt .

i have got a text box for entering email in the same page..

Code: Select all

if(document.form.email.value=="")
{
alert ('Please enter Email.');
return false;
}
the above code is working . if the field is left empty, it will display alert message..

but what i need is , if the user is not entering a valid email id , ie email shhould be in the form abc@sss.com
if not like tat , it should dipsplay message..

what change should i make in the code for that to work??

kindly help pls.

Re: validating fields in a text box - how?

Posted: Tue May 19, 2009 9:13 am
by jayshields
Search Google for Javascript email validation. There are plenty of regular expressions around for doing just what you want. You could go one better and use jQuery with the validate plug-in.