jhunlio
jhunlio

Reputation: 2660

check if input is empty then the default value will be send to the database

I have a form that have ability to set default value if the user will not input anything.

form

<form>
  <input type="text" class="pmi" name="pmi" value="" />
  <input type="text" name="birth_date" value="" />
  <input type="text" name="admission_date" value="" />
  <input type="submit" name="submit" value="submit" />
</form>   

I want only the default value work if the input are empty.

jquery

$('input[name=submit]').click(function (){
            $(".pmi").val("0");
            $("input[name=birth_date]").val(d.yyyymmdd());
            $("input[name=admission_date]").val(d.yyyymmdd());

        });

Upvotes: 1

Views: 252

Answers (3)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Try like this

!$(".pmi").val() && $(".pmi").val("0");
!$("input[name=birth_date]").val() && $("input[name=birth_date]").val(d.yyyymmdd());
!$("input[name=admission_date]").val() && $("input[name=admission_date]").val(d.yyyymmdd());

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

You can check

$('input[name=submit]').click(function () {
    if ($(".pmi").val() == "") {
        $(".pmi").val("0");
    }
    $("input[name=birth_date], input[name=admission_date]").val(function (i, val) {
        return val || d.yyyymmdd();
    });
});

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You can do like this:

$('input[name=submit]').click(function (){
if($(".pmi").val()==""){            
   $(".pmi").val("0");
}
if($("input[name=birth_date]").val() == ""){
   $("input[name=birth_date]").val(d.yyyymmdd());
}
if($("input[name=admission_date]").val() == ""){
   $("input[name=admission_date]").val(d.yyyymmdd());
}
});

Upvotes: 1

Related Questions