Solved

Javascript Error form validation

Posted on 2007-11-15
14
315 Views
Last Modified: 2013-11-19
Hi

I am having a very strange problem with a form validation script I'm using on the following page, it reports that value is undefined - which I thought was due to an incorrectly named element but it all appears to be ok.

http://app.kambee.com/?i=100-39

Error: value is not defined
LINE 14

The error is when the form is submitted - Another set of eyes would be greatly appreciated!

0
Comment
Question by:logicjb
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 20294132
Hard to see the problem from only your question...how about some code?
0
 

Author Comment

by:logicjb
ID: 20294145
I gave a link to the page above, is it stripped from the question?
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20294195
If the problem is in an ASP page, we will not be able to see that from your website :-( You will have to post it here.
0
 

Author Comment

by:logicjb
ID: 20294212
It's not in an ASP page, it's in the HTML source which you can view from the URL above? This is a javascript related question - not ASP.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20294233
logicjb,

The problem is checking for value with the radio buttons.  The radios actually end up being a collection.  You need to loop them, see which is checked, and then get the value of the checked button.  I can help with the specifics if you need it.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:logicjb
ID: 20294251
Thanks b0lsc0tt, that's a great help.

How would one loop the values?  - apologies if that's a pain to explain
0
 
LVL 12

Expert Comment

by:_Maddog_
ID: 20294271
Hi,

You're trying to check the "value" of two radio controls.
thisform.q70 is an array of two radio controls, it does not have a value attribute.

Look into radio controls validation, here's a sample:
http://www.codeave.com/javascript/code.asp?u_log=7049

validate_form, calls validate_required, with thisform.q70 as a parameter, it fails while checking this.q70.value, because it is an array of two radio controls.

- Maddog
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20294284
Let me work on some code.  It is easier to show than explain.  In the meantime to test what I mean add an alert in the first function like ...

function validate_required(field,alerttxt)
{
alert(field[0].value);

If the Yes is checked in the first question you will see the value.  Basically the loop will make that work for all of the radios.

bol
0
 

Author Comment

by:logicjb
ID: 20294444
Thanks bol, the problem does indeed go away if I remove validation for radio - and checkboxes too though.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20294545
Try changing your first function too ...

(see snippet)

It is a little hard for me to test this but that is close.  I have some alerts and you can remove those if needed.

Let me know the results.

bol
function validate_required(field,alerttxt) {

      with (field) {

            alert(length);

            if (length > 1) {

                  for (var item in length) {

                        if (item.checked) {

                              alert(item.name + '-' + item.value);

                              while (item) {

                                    if (value==null||value=="")

                                      {alert(alerttxt);return false}

                                    else {return true}

                              }

                        }

                  }

            } else {

                  alert(name + '-' + value)

                  if (value==null||value=="")

                    {alert(alerttxt);return false}

                  else {return true}

            }

      }

}

Open in new window

0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20294600
One possible issue remaining.  Do you want to require a radio or check box to be checked?  The current script won't but I can add it.

bol
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 20294699
I had to get rid of some of the with()'s you were using.  The code should now alert if a set of radios or checkboxes aren't checked.  The focus won't work on those fields though and it seems to still submit when it shouldn't but that may just be my test page.

The revised function is below.

bol
function validate_required(field,alerttxt) {

	if (field.type == 'text') {

		with (field) {

			if (value==null||value=="")

			  {alert(alerttxt);return false}

			else {return true}

		}

	} else {

		var chked = false;

		for (var i=0; i< field.length; i++) {

			if (field[i].checked) {

				chked = true;

				break;

			}

		}

		if (!chked) {alert(alerttxt);return false}

	}

}

Open in new window

0
 

Author Closing Comment

by:logicjb
ID: 31409473
Superb, thank you.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20299634
I'm glad that did it.  Thanks for the fun question, the grade, and the points.  See you around. :)

bol
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now