Javascript: Validate textboxes to ensure one textbox has a specific string

Hello,

I'm trying to have javascript ENSURE that one of the autogenerated textboxes has a string called 'email'. The catch is it COULD be any of the textboxes.

The textboxes are created depending on the number of elements in a php array.. So:

<form action="index.php" method="post" name="importList">
<?php
foreach ($headerGrabbed as $headerElement) {
            $out.=
            '<tr class="'.((++$tr)%2==1?"rowdark":"rowlight").'"><td>'.$headerElement.' </td><td>
                        <input id="'.$headerElement.'" value=\''.$headerElement.'\' name=\'headerListElement["'.$headerElement.'"]\'></td>
                        <td><input type="checkbox" value="1" id="'.$headerElement.'X2" name=\'headerListElementX2["'.$headerElement.'"]\'></td>
             </tr>';
}
?>
<input type="submit" class=button value="Start Import" name="submitXc">
</form>

Would be the general idea here. All the textboxes end up in a array called 'headerListElementX2' in this case.

What I want to do is check through ALL of them when submit is entered, and make sure one of them has the value of 'email' (Typically this is added by default but they may change it). How could this be done, is it possible using the coding above? Thanks
LVL 7
VallerianiAsked:
Who is Participating?
 
COBOLdinosaurCommented:
I am assuming you want client side validation before submitting:

Change the submit to just a button and use an onclick event to submit:
<input type="button" class=button value="Start Import" name="submitXc" onclick="if (testEmail()) {document.form.importlist.submit();}">

Open in new window



Then the script goes in the head
<script type="text/javascript">
function testEnail()
{
   testSet= document.getElementsByTagName('INPUT');
   for(i=0;i<testSet.length; i++)
   {
        if (testSet.value='email')
        {
            return true;
        }
    }
    // you probably want a message to the user here
    return false;
}
</script>

Open in new window



Cd&


0
 
AngryBinaryCommented:
If you just want to see if a parameter exists in the POST vars, simply use:

if (isset($_POST["email"])) {
   //do some stuff
}

That will tell you if there is any input named "email" in the submitted form. Is that what you're after? It's a little hard to tell.
0
 
VallerianiAuthor Commented:
document.getElementsByTagName('INPUT');, works awesome!!! Thank you very much!

This is what I needed!
0
 
COBOLdinosaurCommented:
Glad we could help :^)


Cd&
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.