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


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">
foreach ($headerGrabbed as $headerElement) {
            '<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>
<input type="submit" class=button value="Start Import" name="submitXc">

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
Who is Participating?
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;

Open in new window


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.
VallerianiAuthor Commented:
document.getElementsByTagName('INPUT');, works awesome!!! Thank you very much!

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

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.