0

How do you validate a form but also get information from one form to the other using JavaScript?

I can get the information from a field using the getElementById, but i cannot seem to validate the form.

    //Iam trying to build a drivers license renewal form
    //This is my JavaScript code
    //Iam retrieving from one form and showing it on a different form. 

        function validateForm(){  // i tried this code but it doesnt seem to work

        var x = document.forms["licenceform"]["surname_value"].valueOf;
         if (x == null || x == "") {
            alert(" Surname name must be filled out");
            return false;
        }
    }
Sid Raju
  • 1
  • 1
  • can you post your markup and any script you have attempted? – Donal Sep 04 '14 at 00:46
  • @Donal its up. please check it. Iam trying to build a drivers licence renewal form. i can retrieve information but cannot check if the fields are empty then show error messege – Sid Raju Sep 04 '14 at 09:51
  • Have a look here: http://stackoverflow.com/questions/16134733/html-javascript-simple-form-validation-on-submit – Donal Sep 04 '14 at 10:54

1 Answers1

0

How do you validate a form

Typically by adding a listener for the form's submit event, then checking that the values of form controls fit your validation criteria. You can also check values as they are entered or when focus moves off a control and it's been changed.

There is no need for getElementById, all form controls with a name are available as properties of the form and in the form's elements collection, so you just iterate over that and check values as you go.

but also get information from one form to the other using JavaScript?

If the second form is in the same page, you can get set the value of a form control based on the value of some other form control, either in the same form or another.

If the second form is in a different page, the easiest method is to submit the first form, read the value at the server and set it in the second form before sending it to the client.

Once you have made an attempt at the above, post some code here if you have further questions.

RobG
  • 142,382
  • 31
  • 172
  • 209