-2

I want to input only numbers while entering and restrict characters.

{!! Form::input('text','advertisement_height',null,['class'=>'form-control']) !!}
user229044
  • 232,980
  • 40
  • 330
  • 338
Sajin Shereef
  • 119
  • 1
  • 1
  • 7
  • 1
    Did you bother to research how to do this first…? Laravel has numeric validation, and there’ll be hundreds of JavaScript libraries that restrict input to numbers only. – Martin Bean Feb 02 '16 at 12:55
  • Check the answer posted here. http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input – mbklnd Feb 02 '16 at 14:45

5 Answers5

1

You could change the input type to a number field.

<input type="number" />
mbklnd
  • 131
  • 1
  • 11
1

The Laravel validation class comes with a bunch of different number validators: https://laravel.com/docs/5.2/validation#available-validation-rules

You can see there is integer and numeric. If you have the request in your control you could just write the rules for it.

// on your controller
public function store(Request $request)
{
  $rules = ['advertisement_height' => 'required|numeric'];
  $this->validate($request, $rules);
}

or you can create a validation object and run the validation on your input, however you get it.

Tutelage Systems
  • 1,724
  • 1
  • 9
  • 6
1

Try it here!

<input id="txtnum" onkeypress="return isNumber(event)" type="text" name="txtnum">

Note that digits are allowed and alphabetic characters are not allowed. Observe, too, that arrow keys and backspace are allowed so that you can still edit what you type.

function isNumber(evt)
  {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
  }

Possible Answer: Allow only numbers to be typed in a textbox

Fiddle: http://jsfiddle.net/Lm2hS/

yogesh chatrola
  • 429
  • 4
  • 11
0

Instead of "text" say "number":

Form::input('number'....

I think Form::number()... doesn't exist any more

user229044
  • 232,980
  • 40
  • 330
  • 338
MasterSith
  • 175
  • 6
0

{{Form::number('name','default_text',array('class' => 'form-control', 'required'))}}

use 'number' instead of 'text' and 'required' attribute for mandatory field.

Popeye
  • 253
  • 4
  • 13