0

I wanted my text field to accept only numbers so I try to use HTML5 attribute using "number". But it removes the value inside my text field. here is my code

 <div class="form-wrapper-full-width form-wrapper">
   <div class="form-label">
     Company Business Number:
   </div>   
   <div class="form-info">
       <input type="number" name="business_number" title="Business Number" value="<?=$phone_num['business_number']?>">
   </div> 
 </div>
RJParikh
  • 4,096
  • 1
  • 19
  • 36
Drenyl
  • 906
  • 1
  • 18
  • 41

4 Answers4

2

Its not removing your value but you forgot to echo your value and thats why it showing blank. echo your value.

Make sure your value must be a number if it contains any special character or anything apart from number then it will remove that value and shows blank.

Try below code:

<div class="form-wrapper-full-width form-wrapper">
   <div class="form-label">
        Company Business Number:
   </div>           
   <div class="form-info">
       <input type="number" name="business_number" title="Business Number" value="<?php echo $phone_num['business_number']; ?>">
   </div>   
 </div>
Manthan Dave
  • 2,137
  • 2
  • 17
  • 31
  • No. He has not forget to echo the value because `=$phone_num['business_number']?>` is equal to ``. – Ranuka Dec 08 '16 at 03:23
  • @Ranuka if short tags of php not allowed from php.ini then definetelly it will not works and thats why i have write echo the value which you are getting. hope it clears to you i guess. – Manthan Dave Dec 08 '16 at 04:52
2

Are you sure that a value is set to $phone_num['business_number']? And are you sure that $phone_num['business_number'] contains only numbers?

If it is set just try removing short tags of PHP.

<div class="form-wrapper-full-width form-wrapper">
   <div class="form-label">
        Company Business Number:
   </div>           
   <div class="form-info">
       <input type="number" name="business_number" title="Business Number" value="<?php echo $phone_num['business_number']; ?>">
   </div>   
 </div>
Ranuka
  • 273
  • 3
  • 17
  • yea it's the problem, it's contains "+" sign that's why it's becoming blank. how can I solve that? – Drenyl Nov 22 '16 at 05:52
  • You have to use patterns. There is a answer in this thread. http://stackoverflow.com/questions/32259058/allow-sign-on-html-form-type-number – Ranuka Nov 22 '16 at 06:03
  • Check this also. http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input?noredirect=1&lq=1 – Ranuka Nov 22 '16 at 06:08
  • thanks, honestly those link didn't solve my problem, but you help me to figure it out. – Drenyl Nov 22 '16 at 08:22
1

Using Javascript you can remove text and can keep numbers.

<?php
    $business_number = "100ww";
?>
<div class="form-wrapper-full-width form-wrapper">
<div class="form-label">
    Company Business Number:
</div>          
<div class="form-info">
    <input type="text" id="business_number" name="business_number" title="Business Number" value="<?=$business_number?>">
</div>  
</div>

<script type="text/javascript">
function changeIt(){
    var val = document.getElementById('business_number').value;
    number = val.replace(/\D/g,'');
    document.getElementById('business_number').value = number;
}
changeIt()
</script>
Gayan
  • 2,845
  • 7
  • 33
  • 60
0

First check any js aur css apply on your input fields also check short tag is allow in your php.ini configuration.