0

I have a text input box for entering user name. i want to restrict the special characters and numbers from being typed in that box(not showing the error after the submit button click. avoiding typing and pasting from keyboard and mouse). I came across some javascipt codes to restrict on the keyboard and mouse events (like keypress etc.,). but i think there might be better way than doing this by adding some html attributes or by adding some bootstrap framework classes.

I tried the below code (ruby on rails):

<%= f.text_field :fname,  required: true, placeholder: "First Name", class: "form-control",:pattern => "^[a-zA-Z0-9]+$" %> 

However this is triggering the error after submit button click

I am using JQuery in my application.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
John
  • 1,273
  • 3
  • 27
  • 61

1 Answers1

0

You can use input masks. There are many jquery plugins which will help you do so.

Check this stackoverflow question for demo and answers:

How to restrict special characters in a certain form field

ashfaq.p
  • 5,379
  • 21
  • 35