Benny
Benny

Reputation: 55

Form onsubmit issue with javascript

Ok so I am really confused on this whole javascript in HTML stuff. What I am trying to do is validate a form either "onblur" or on submit with an external file.

Here is the HTML code that works for the first field:

    <script>
    function notEmpty(rep, errMsg)
    {
    var errMsg = "Please enter something in Rep";
    var rep = document.getElementById('submitted_by_hrrep');
    if(rep.value == '')
    {
    alert(errMsg);
    hrrep.focus();
    return false;
    }
    return true;
    }
    </script>

This is in the body of the form near its field.

<script type="text/javascript">document.getElementById("submitted_by_rep").onblur=notEmpty;</script>

So that DOES work and will pop an alert that tells em to go back What I CAN'T get to work is doing this for the rest (15 fields) of the form. The "onsubmit" is confusing me and I think it's right but I am not sure.

<form onsubmit="return formValidation()" method="post" action="process.asp" >

Anything will help

EDIT

    function validate()
    {
    if(document.newempRequest.submitted_by_hrrep.value ==='')
    {
    alert("Please provide your name");
    document.newempRequest.submitted_by_hrrep.focus();
    return false;
    }

I got so frustrated that I started from scratch and took it a field at a time. Found that this works for the fields that need text, it looks messy for the file but calling it externally works flawlessly.

I wish I could use jquery but it seems to be more complex to setup that I actually need. Thanks for the help :)

Upvotes: 0

Views: 199

Answers (1)

haz0rd
haz0rd

Reputation: 270

You would have to grab all the inputs then iterate over them with a loop of some flavor, maybe a for loop?

with jquery it's really easy since there are already form validator plugins out there, and the selectors are really friendly. Using jquery,

$('#formId input') 

would grab all the inputs in the form, then you can use a .each() to iterate through all the inputs

You obviously aren't going to be able to .focus() on all of the fields though, so need another function to handle the entire list instead of just one.

Upvotes: 1

Related Questions