Help - Search - Members - Calendar
Full Version: validating field values
HTMLHelp Forums > Programming > Client-side Scripting
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
<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
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...
geoffmerritt
How far do you want to go in checking the dates.... do you want to also check that Feb has 28 days except every 4th year and that March has 31.... etc

The simplest method would be to create a drop down list with only the Days (1-31) Months (Jan-Dec) and years....

This way the user has only the options available and therefore can not submit anything else.

If you have select in the first line as below.... you can use your code to check that the "Select" hasn't been used and a date added.
CODE

<select name="select" size="1">
  <option value="Select" selected>Select</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

CODE

document.form.date.value=="Select"


Christian J
QUOTE(rrn @ May 18 2009, 12:08 PM) *

there is a 3 text boxes for entering date of birth

CODE
<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>


Just make sure the users understand the order of the day month and year fields (the order varies in different parts of the world).

CODE
date should be a value between 1 and 31.

You must also check that it's an integer.

Possibly regular expressions can be used: http://www.regular-expressions.info/dates.html but like geoffmerritt writes it seems much simpler to use a SELECT menu instead of text fields for this. You must still check the submitted data in the server-side script, though.
Brian Chandler
Actually though, entering a date by selecting from drop-downs is terribly tedious. *Much* faster if you're just allowed to type the numbers. And since the drop-down method doesn't actually validate anything, why not go for typing in numbers.

Other points:
As Christian says, the date order can be Y-M-D (ISO, and lots of places), D-M-Y (lots more places, or M-D-Y (um, one or two places), so you need to be specific.

But I think "Type the date" is confusing: the 'date' means the whole thing, so I think 'day' is clearer.

Try looking for a javascript date validation function. (In PHP I would just use the date to timestamp converter (which normalises the date), and format back, to see if I get the same answer. If not, it was an invalid date.
rrn
Thank u all for ur help...

i have got one more doubt .

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

CODE
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..
geoffmerritt
Below is some code that uses Regular Expressions to check the email data being entered, http://regexlib.com/ is a site that has lots of examples for checking different types of data.

The same section of code can be used to check the dates as discussed above, by changing the regexp.
CODE

function Contact()
        {
        re = "no";
        remail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if(remail.test(document.contact.email.value))
                {
                re="yes"
                }
            else
                {
                alert("Please check your email address");
                document.contact.email.focus();
                document.contact.email.select();
                return false
                }
        if(re=="yes")
        {
        return true
        }
        }
Darin McGrew
QUOTE
remail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
This regular expression will reject some valid email addresses.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.