2
votes

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.

1

1 Answers

2
votes

Use the pattern attribute that accepts javascript regular expressions.

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