0

i validation form like this:

  • cannot give comma
  • cannot give minus(-)
  • cannot give 0 number
  • cannot give blank number
  • cannot give dot(.) character

this is my code:

<input type="number" required min="1" step="1" pattern="^(\d+\.)?\d+$" />

how pattern to prevent that?

1 Answer 1

1

From what I checked, it seems that it may depend on browser. With following pattern, Chrome allows numbers only, Firefox marks numbers with coma or dot as invalid and IE allows [a-z] as well.

^([1-9]|\.|\,)+([0-9])*(\.|\,)?([0-9])*$

I would add onchange event and set it up with javascript, because I can't see pattern, which will allow/disallow 0 based on first character and will remove browsers differences. 0,1 is still number.

Sign up to request clarification or add additional context in comments.

3 Comments

how to prevent this type like this 000,001.
So you can, but don't want be able to type those?
<input type="text" required="required" pattern="^((([1-9])+(\.|\,)?([0-9])*)|(0(\.|,)[0-9]+))$" /> — but this works only on FireFox. If I change type to text, Chrome allows a-z.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.