iamjonesy
iamjonesy

Reputation: 25132

Form validation - help with rules

Hi I'm writing and app in PHP. I want to use jquery form validation but the situation I have are 'from' and 'to' fields. If one of these used and the other is blank I want to stop the form submit.

So user can only submit the form if to and from are either both empty or both contain dates.

Here are the fields:

<table class="search-form">
<tr>
    <td><label>Client</label></td>
    <td></td>
    <td><?php echo form_dropdown('client', $clients, $current_client);?></td>
</tr>
<tr>
    <td><label>Date From: </label></td>
    <td></td>
    <td><input type="text" name="from_date" class="datepicker" value="<?php echo $from; ?>"/></td>
</tr>
<tr>
    <td><label>Date To: </label></td>
    <td></td>
    <td><input type="text" name="to_date" class="datepicker" value="<?php echo $to; ?>" /></td>
</tr>
<tr>
    <td></td>
    <td></td>
    <td><input type="submit" name="submit" value="Search" /></td>
</tr>

Can someone steer me in the right direction here.

Thanks,

Billy

Upvotes: 0

Views: 191

Answers (2)

sdm350
sdm350

Reputation: 397

There may be something built into jQuery to handle this, but I can't think of it if there is. You could simply attach a function to the submit button that does some quick checking like this:

function(){ //attached to the $('form').submit
if (($("from_date").text == "" && $("to_date").text == "")
|| $("from_date").text != "" && $("to_date").text != ""))
    return true;
else
    return false; //return false should prevent the form from from being submitted

Upvotes: 1

Eugene msc
Eugene msc

Reputation: 380

You need to add form tag and then in jquery do

$('form').submit(function() {
//validation here
 if(success)return true;
   else return false;
});

return false means that default submit function is not launched

Upvotes: 0

Related Questions