0

Okay i just ask question about how to validate form with condition, now i have new problem, i have input text but it is readonly lets say call input text is address, and i have antoher input lets say call office. When i try input office,the text addres will sow up depends what i write on office. (I'm using ajax and mysql to get address), if that office didnt have data on mysql the address will not show up. Now my problem is on form validate, i need to check if the addres is empty i need show alert. How do i did that?? I try code like this

$('#form').validate({
            errorElement: 'span',
            errorClass: 'help-block',
            rules: {
                office : {
                 required : true
               },
                address: {
                    required: {
                        depends: function(element) {
                            if ($('#office').val() != '') {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    }
                }},
             messages: {
                Office : "Please insert name office",
                address : "Seem office not quite right"
            }
});

and here my html code

<form class="form-filter" id="form" role="form">
   <div class="row">
                <div class="col-md-4 col-sm-5">
                    <div class="form-group">
<label>
                            Office
                        </label>
                        <input type="text" placeholder="Insert name Office" class="form-control" id="Office" name="Office">
                    </div>
                    <div class="form-group">
                        <label>
                            Address
                        </label>
                        <input type="text" class="form-control" id="address" name="address" readonly>
                    </div>
<button type="submit">Check</button>
</from>

but when i try to submit it didnt work at all. If i try not filled my office it work prefectly. Or readonly cant be use on rules???

Upadte, is kinda buggy if i use readonly. So i try another solution i'm gonna try use even click button then.

Update again!! Okay i'm using button even click. and is work. so i just need check like this

$('#btnsave').click(function() {
  if($("#form").valid()==true)
  {
    if($('#address').val()!="")
    {
       do something;
    }
    else
    {
       do alert;
    }
  }
});
Wolfzmus
  • 463
  • 1
  • 10
  • 23
  • You want to make a read-only input required? That doesn't make sense. – jeroen Oct 27 '16 at 07:49
  • i know is liitle bit weird to oyu, but i nedd that, cause i need too check that office have addres or not. – Wolfzmus Oct 27 '16 at 07:51
  • So check if the office is valid directly. No need to do that through another input. – jeroen Oct 27 '16 at 07:52
  • okay tq, i'm edit my question, i'm gonna use even click then, tq for your suggestion :) – Wolfzmus Oct 27 '16 at 07:53
  • And what happens when the address is missing? I mean the user gets an error message but he can't enter anything since the address field is read-only. – simon Oct 27 '16 at 07:54
  • if you are using ajax to retrieve the address you can trigger the validation on the ajax call. if you don't get a result you show the warning. No need to wait to submit the form to get the validation of this field – Lelio Faieta Oct 27 '16 at 07:55
  • You can check if the address is empty and show the warning as well as disabling the submit button to stop the form from submitiing. – Abdullah A Malik Oct 27 '16 at 08:09
  • @AbdullahAMalik thats i'm gonna do, i'm using button even click, so i can check if form.valid is true and check again if addres not empty. – Wolfzmus Oct 27 '16 at 08:27
  • @Wolfzmus Can you please explain `if($address!="")` what does it mean? – AddWeb Solution Pvt Ltd Oct 27 '16 at 10:08
  • @AddWebSolutionPvtLtd : is same thing like on php or mysql like $addres<>"", but on jquery u cant use "<>". But if u mean $address not variable, well yes i'm kinda typo that lol. I'm edit that – Wolfzmus Oct 27 '16 at 10:10

1 Answers1

0

I have place a some code hack but you need to manage it as per your code. So test it first.

$('#address').prop('readonly', false);//Convert readonly while validation perform
//Do your validation stuff
$('#address').prop('readonly', true);//again make it readonly

Let me know if there is any confusion.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57