Kevin
Kevin

Reputation: 407

HTML5 Form Validation

Is there a way to use HTML5's built in form validation in input elements that are NOT required? I.e. is there a way to validate an HTML5 input if and only if the field has an actual value?

For example, I'd like to use the following markup to check whether some_name is a URL if and only if the user actually enters a value in the input. If the user leaves the input blank, the form should still be able to submit as usual.

<input type="url" name="some_name" [some attribute or additional markup?]/>

Thanks very much for your help.

Upvotes: 2

Views: 375

Answers (1)

Johan Sund&#233;n
Johan Sund&#233;n

Reputation: 447

Use the pattern attribute that accepts javascript regular expressions.

<input type="url" name="some_name" pattern="your regular expression"/>

Upvotes: 2

Related Questions