Patrick Lemark
Patrick Lemark

Reputation: 43

Hide zero values of an empty input(number type)

I have a form that gives me data from database, i have number input type. By default it is "0" showed for empty entries. I want to hide "0" from the field and show the value just if is different of 0.

I tried with the code below but it doesn't work.

 <input data-validate="number"  value="<?php echo $value; ?>" class="form-control" onload="if(this.value  == '0') { this.value = ' '; } " >

Upvotes: 4

Views: 3630

Answers (2)

Jeremy Young
Jeremy Young

Reputation: 304

I wrote a minimal php function to do this

function fnB($input){
//function to make values blank in number input boxes 
if ($input==0) $r="";
else $r=$input;
return $r;}
?>

So in the form one can then enter

 value = <?php echo fnB($value);?>

Upvotes: 1

antyrat
antyrat

Reputation: 27765

Add ternary operator to PHP block instead:

<input data-validate="number" value="<?php echo ($value != '0' ? $value : ''); ?>" class="form-control">

Upvotes: 2

Related Questions