salimagic salimagic - 4 months ago 17
HTML Question

Validate form being submitted using javascript

so this might be something super simple to do, but I can't seem to get it. I have a form and a validation for it. The validation text works. If I use a normal submit button the form works correctly, but if I use javascript to submit the form, it does not work 100%.

<form id="transport" name="transport" method="post" action="submit_trans_request.php">
<input type="text" name="f_name" value="<?php echo htmlspecialchars($row['f_name'], ENT_QUOTES); ?>" style="font-size:16px;" />
<a href="javascript:onclick=validateForm(); document.transport.submit();" class="submit_btn">Submit</a>

So basically I am looking for a way to stop the submit if
returns an alert. Not sure how I go about that.

Any help is greatly appreciated. Thank you in advance!


You could do it the way you've designed it by just returning false in the validationForm().

If I were you, I would just call whatever method you want when the user clicks submit, then inside of that method, call the validationForm() method. If that returns true, go forward with the process, otherwise alert the user.

Something like this:

   <input type="text" />
   <a href="javascript:onclick=submitData();"/>

Then in the submitData() function, do something like this:

function submitData() {
    var b = validationForm();

    if (b) {
        // submit data
    } else {
        // alert user something entered wrong

Now, in the validationForm() function, you need to make sure that you return true or false.