1

I do not really mastered this, but i want to validation checks form HTML attribute, that is "pattern", this example my own:

<input type='text' pattern='[-+]?[0-9]*[.,]?[0-9]+' />

This validates only one number. How can I validate a comma-separated list of numbers?

8
  • What is the requirement here? Please provide input strings you want to match and those you do not want to match. Commented Mar 29, 2017 at 13:49
  • what? i want to create HTML validation for my <input> tag, using by pattern. in some tag i want to using ONLY Commas and Number, just that.., i dont understand you problem @WiktorStribiżew Commented Mar 29, 2017 at 14:20
  • Ok, is 11,22,33,44,1, or 1,,,,3 valid? Commented Mar 29, 2017 at 14:41
  • i want is 11,22,33,44, and not 1,,,,,3 :) @WiktorStribiżew Commented Mar 29, 2017 at 15:08
  • 1
    See my explanations. No need to be excited about it that much, it is just a regex. Commented Mar 29, 2017 at 15:17

1 Answer 1

1

You seem to want to validate a string of comma-separated numbers that meet your pattern.

Use

pattern="[-+]?[0-9]*[.,]?[0-9]+(?:,[-+]?[0-9]*[.,]?[0-9]+)*"

The pattern is anchored by default, so no need using ^ at the start and $ at the end.

Details:

  • [-+]? - an optional - or +
  • [0-9]* - 0+ digits
  • [.,]? - an optional . or ,
  • [0-9]+ - 1+ digits
  • (?:,[-+]?[0-9]*[.,]?[0-9]+)* - zero or more sequences of:
    • , - a comma
    • [-+]?[0-9]*[.,]?[0-9]+ - the number pattern (described above).

See a demo below:

input:valid {
  color: green;
}
input:invalid {
  color: red;
}
<form name="form1"> 
 <input pattern="[-+]?[0-9]*[.,]?[0-9]+(?:,[-+]?[0-9]*[.,]?[0-9]+)*" title=""/>
 <input type="Submit"/> 
</form>

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

Comments

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.