user2367083 user2367083 - 1 year ago 76
Javascript Question

Jquery form submit & textbox validation

I have a form.

The form has a textbox and a submit button, that's all.

I want the button to validate that the textbox isn't empty/blank before causing the page to trip; before posting the page.

1) if button clicked then if textbox is blank then change textbox background color & don't post the page back to the server.

2) if button clicked then if textbox is not empty post the page back to the server.


Answer Source

Something like this should get you started:

  var in1 = $('#in1').val();
  if (in1==""){
    return false;
<script src=""></script>

<form action="another_php_file.php" method="post">
  <input id="in1" name="in1" type="text" />
  <button id="btnSubmit">Submit</button>

You may already know this, but JIC: Where the form gets posted back to depends on the contents of the action= attribute on the form tag.

<form action="sumother_php_file.php" method="post">

Will send the form to a PHP file called sumother_php_file.php

<form action="" method="post">

Will post the form to itself (You would add PHP code between <?php //tags ?> to manage the question of whether the variable $_POST['in1'] isset or not)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download