473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Form field validation errors

Ok ,i have to create a page to allow entry of user information;all fields must be validated and error displayed otherwise

* First Name (limit 25 characters)
* Last Name (limit 25 characters)
* Street Address (limit 45 characters)
* City (limit 25 characters)
* State (2 character code )
* Zip Code (5 digits only, must be numeric)
* Date of Birth (mm/dd/yyyy format, day, month, and year must be valid)

I am been trying to validate all fields, but i got a hard time doing it ...
This is the code so far :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<SCRIPT LANGUAGE="JavaScript">
function isValidDate(dateStr) {

var datePat = /^(\d{1,2})(\/|)(\d{1,2})\2(\d{4})$/;


var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true; // date is valid
}
// End -->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label>First Name
<input name="First" type="text" id="First" maxlength="25" />
</label>
</form>
<form id="form2" name="form2" method="post" action="">
<label>Last Name
<input name="Last" type="text" id="Last" maxlength="25" />
</label>
</form>
<form id="form3" name="form3" method="post" action="">
<label>Street Address
<input name="Street" type="text" id="Street" value="" maxlength="45" />
</label>
</form>
<form id="form4" name="form4" method="post" action="">
<label>City
<input name="City" type="text" id="City" maxlength="25" />
</label>
</form>
<form id="form5" name="form5" method="post" action="">
<label>State
<select name="State" size="1" id="State">
<option>FL</option>
<option>GA</option>
<option>NY</option>
</select>
</label>
</form>
<form id="form6" name="form6" method="post" action="">
<label>Zip
<input name="Zip" type="text" id="Zip" maxlength="5" />
</label>
</form>

<form id="form8" name="form8" method="post" action="">
<label>Submit
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label>
</form>


Your Birth Date:
<input type=text name=date size=10 maxlength=10> (in MM/DD/YYYY format)
<input type=submit value="Submit Date">
</form>
</body>
</html>

Any help will be appreciated.
May 30 '07 #1
8 2255
RedSon
5,000 Expert 4TB
No one will help you until you read the posting guidelines.

Posting Guidelines
May 30 '07 #2
Sorry , i will post it again !!!
May 30 '07 #3
I have to create a site that have fields to put the following info and input must be validated and error displayed otherwise.

* First Name (limit 25 characters)
* Last Name (limit 25 characters)
* Street Address (limit 45 characters)
* City (limit 25 characters)
* State (2 character code )
* Zip Code (5 digits only, must be numeric)
* Date of Birth (mm/dd/yyyy format, day, month, and year must be valid)

I have the site created but the validation is not working
THE RELEVANT PART OF THE CODE IS HERE:

<script language=JavaScript src="mycheck.js" type="text/javascript"> </script>
</head>

<body>
<form name="form1" method="post" action="" onSubmit="Validation(this)">

<p>First:
<input type="text" name="fname" maxlength="25" size="25"></p>

<p>Last:
<input type="text" name="lname" maxlength="25" size="25"></p>
<p>Address:
<input type="text" name="address" maxlength="45" size="45"></p>
<p>City:
<input type="text" name="city" maxlength="25" size="25"></p>
<p>State:
<select name="state">
<option value=""></option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>

</select></p>

<p>Zip:
<input type="text" name="zipcode" maxlength="5" size="5"></p>

<p>Date of Birth:
<input type="text" name="txtDate" maxlength="10" size="10"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>

</body>

THIS IS MYCHECK.JS FILE:

SCRIPT LANGUAGE="JavaScript">
function isValidDate(dateStr) {

var datePat = /^(\d{1,2})(\/|)(\d{1,2})\2(\d{4})$/;


var matchArray = dateStr.match(datePat);

if (matchArray == null)
{
alert("Null entry date.")
return false;
}

month = matchArray[1];
day = matchArray[3];
year = matchArray[4];

if (month < 1 || month > 12)
{
alert("Wrong entry , 1 and 12 value needed!!!");
return false;
}
if (day < 1 || day > 31)
{
alert("Wrong entry 1 and 31 value needed !!!");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
alert("Wrong entry, 31 days needed!")
return false
}
if (month == 2)
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("Wrong entry ,February!!!");
return false;
}
}
return true;
}

// End -->
</script>


Any help will be appreciated.
May 30 '07 #4
JosAH
11,448 Expert 8TB
Remember: Javascript isn't Java. I'll move this to the appropriate Javascript forum.

kind regards,

Jos
May 31 '07 #5
JosAH
11,448 Expert 8TB
I just moved your other thread to the Javascript forum where it belongs. Java is
not Javascript and this forum is a Java forum. We can't help you here. I'll close
this thread; please continue your discussion in the Javascript forum where you'll
find your other thread again. Thanks.

kind regards,

Jos
May 31 '07 #6
acoder
16,027 Expert Mod 8TB
Changed thread title.
May 31 '07 #7
acoder
16,027 Expert Mod 8TB
You've got 8/9 forms - you only need one. Get rid of all the extra form tags.

Also, read up on validation by checking out the Form validation links in the Offsite Links thread at the top of the Javascript forum.
May 31 '07 #8
acoder
16,027 Expert Mod 8TB
Threads merged.
May 31 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: TekWiz | last post by:
I've got a system that automatically generates a form. I have it set up so that the backend will return to the inital form page with an error object in sessions data (assuming the backend detected...
6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
10
by: Steve Benson | last post by:
Our regular programmer moved on. I'm almost clueless in Javascript/ASP and got the job of adapting existing code. In the page below, everything works until I added the function checkIt() to...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
3
by: Adrock952 | last post by:
I have a form which validates fine using classes but i would like the fields that have the error to be highlighted a different color At the moment I just get a list of errors but would like a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.