PDA

View Full Version : can't get validate to work



mcolton
06-18-2014, 09:06 PM
I have a form with 3 phone numbers. One of the 3 is required. Why doesn't this work.
My first line of the form is:


<form id="infoupdateform" method="post" action="xxx.php" onsubmit="validateform()">


<script type="text/javascript">
function validateform(){
if ((document.getElementByName('home').value == '') && (document.getElementByName('cell').value == '') && (document.getElementByName('work').value == ''))
{
alert('At least 1 phone number is required');
document.getElementById('home').focus();
}
}
</script>

Thanks for any help

jscheuer1
06-19-2014, 01:42 AM
There could be other problems. But when validating via the onsubmit event, the return value is what determines whether or not the form submits (add the red highlighted, two places) and, when using document.getElementByName, the 0 based index of the element within the collection of all elements that might have that name must be supplied (add green highlighted, 3 places):


<form id="infoupdateform" method="post" action="xxx.php" onsubmit="return validateform();">

<script type="text/javascript">
function validateform(){
if ((document.getElementByName('home')[0].value == '') && (document.getElementByName('cell')[0].value == '') && (document.getElementByName('work')[0].value == ''))
{
alert('At least 1 phone number is required');
document.getElementById('home').focus();
return false;
}
}
</script>

This assumes the form and page has three text input fields, 1 and only one (or at least the first of) each with a name of 'home', 'cell', and 'work' in the form.

The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.


Note: There can be more than one element on a page with a given name, that's why the index must be specified.

mcolton
06-19-2014, 11:38 AM
didn't work. I put in an alert and I got to the validate but not the "if".
I've been reading about getelement online for 3 days and I get more confused each day. I've also tried 30 varieties of validate and none worked.

My code is at www.lotatennis.com (http://lotatennis.com). Go to the very bottom left and click "testing". Enter Marty Colton, submit. All 3 phone numbers are blank. Hit submit and it should go to validate. It just goes back to the front page. BTW, I used home, cell, and work in the example above but they are really zhome, zcell, and zwork. The online code does match.

Thanks for the help. I'll be gone for 3-4 days.

jscheuer1
06-19-2014, 03:45 PM
It shows. There is no getElementByName, sorry I missed that. There is a getElementsByName. This code works:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>
<form id="infoupdateform" method="post" action="xxx.php" onsubmit="return validateform();">
Home: <input type="text" name="home">
Cell: <input type="text" name="cell">
Work: <input type="text" name="work">
<input type="submit" value="Update">
</form>

<script type="text/javascript">
function validateform(){
if ((document.getElementsByName('home')[0].value == '') && (document.getElementsByName('cell')[0].value == '') && (document.getElementsByName('work')[0].value == ''))
{
alert('At least 1 phone number is required');
document.getElementsByName('home')[0].focus();
return false;
}
}
</script>
</body>
</html>

mcolton
06-23-2014, 10:19 AM
It worked.....partially. it got to the alert statement but the focus statement didn't work. After I hit ok, it returned to my home page.
I also tried getElementsById and I also tried removing the return false statement.

jscheuer1
06-23-2014, 04:31 PM
That's funny, works fine here:

http://home.comcast.net/~jscheuer1/side/demos/tidbits/forms/phonenumval.htm

mcolton
06-23-2014, 04:38 PM
It makes a difference where the validate is. I had it before the form. It works if I put the validate after the form.
Thanks

jscheuer1
06-23-2014, 05:01 PM
In the example page I made, it doesn't matter where the function is. I cannot speak for your page, I haven't seen it. Code cannot execute before elements in the page for which it's looking as it executes have been parsed. And code cannot be assigned to elements that haven't been parsed at the time assignment is attempted. Neither of those cases apply to my simple example code and markup. Again, I cannot speak for the exact code and markup you're using. But it's also possible that it was some other problem that got corrected during the process of moving the code.