Question

Asked on 2011-05-09 at 05:23:22ID: 27360737

Javascript form validation on dynamic forms

by: Lexx877500

Rating9.0Excellent

This links in with a couple of other posts here:

I have this code:  [embed=snippet 8161981]

This validates a form 'extra1' and 'extra2'. The problem is, these are dynamic, and it is possible that those forms never exist. Or that just 'extra1' exists and 'extra2' doesn't.

Javascript doesn't like this, it tries of course to run the code and if it tries to validate a form which doesn't exist, it falls over.

I am trying to come up with a 'catch' which will only run the code if that particular form exists, and skip it if not.

Is there a good way to do this?

if ((document.extra1.title1.value ==="") || (document.extra1.firstname1.value==="") || (document.extra1.surname1.value==="") || (document.extra1.bags1.value==="") ||(document.extra1.bags1.value > 3) || (IsNumeric(document.extra1.title1.value)) || (IsNumeric(document.extra1.firstname1.value)) || (IsNumeric(document.extra1.surname1.value)) || (!IsNumeric(document.extra1.bags1.value)))
{
document.getElementById("error").innerHTML="You need to fill in all details for Extra Passenger 1. Ensure text and numerals have been used as appropiate. You can have a maximum of three bags per passenger.";
valid = false;
}


else if ((document.extra2.title2.value ==="") || (document.extra2.firstname2.value==="") || (document.extra2.surname2.value==="") ||(document.extra2.bags2.value > 3) || (IsNumeric(document.extra2.title2.value)) || (IsNumeric(document.extra2.firstname2.value)) || (IsNumeric(document.extra2.surname2.value)) || (!IsNumeric(document.extra2.bags2.value)))
{
document.getElementById("error").innerHTML="You need to fill in all details for Extra Passenger 2. Ensure text and numerals have been used as appropiate. You can have a maximum of three bags per passenger.";
valid = false;
}
                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

Related Solutions

Solution

Sign up now to view this solution! It's quick, easy, and secure to subscribe. We will return you to this solution, unlocked, when you’re done.

About this Solution

View This Solution - Start Your 30 Day Free Trial
Zone
general
Tags
Javascript HTML
Participating Experts
3
Solution Grade
A

Replies

 by: mayank_joshi

Posted on 2011-05-09 at 05:37:36ID: 36812471

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: mayank_joshi

Posted on 2011-05-09 at 05:40:22ID: 36812472

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: Lexx877

Posted on 2011-05-09 at 05:42:26ID: 36812473

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: tim_cs

Posted on 2011-05-09 at 06:14:48ID: 36812474

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: Designbyonyx

Posted on 2011-05-09 at 06:44:07ID: 36812475

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

Log In

Forgot your password?Sign up

Top Experts

  1. mplungjan

    1,840

    0 points yesterday

    Profile

BrowseAre you an Expert?