?
avatar brianteeman
brianteeman
6 Dec 2017

What

HTML5 input patterns are a way of adding simple validation rules using regexp directly in the form field.

Why

HTML5 input patterns just like the required attribute are checked immediately and not on submit so they provide instant feedback. This not only reduces calls to the server but also improves the usability and accessibility of the form.

Joomla Support

Input patterns are supported by the Joomla text field but they should also be supported by search, tel, url, email, and password fields.

J4 has partial support for custom validation messages but is not using the specification for the value in the xml

Finally although Joomla supports this in the text fields so it will work in a field defined in an xml file there is no UI to be able to use this in a custom field.

Supported in the core

J3 - supports patterns but not custom validation message

https://github.com/joomla/joomla-cms/blob/staging/layouts/joomla/form/field/text.php#L75

J4 - supports patterns and partial (non-standard) validation message

https://github.com/joomla/joomla-cms/blob/4.0-dev/layouts/joomla/form/field/text.php#L72

Already being used

See media-action-crop plugin in J4

Specification

A regular expression that the control's value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is text, search, tel, url, email, or password, otherwise it is ignored. The regular expression language is the same as JavaScript RegExp algorithm, with the 'u'parameter that makes it treat the pattern as a sequence of unicode code points. The pattern is not surrounded by forward slashes.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-pattern

Support

Supported by all browsers see https://caniuse.com/#feat=input-pattern

Basic usage

Add [a-z]{1,15} as the value of the pattern attribute in our username input:

<input
       type="text"
       name="username"
       placeholder="Username"
       pattern="[a-z]{1,15}”>

Now, since it only accepts lowercase letters, submitting any other value will throw an error message immediately

Customising the Validation Message

We can customise the message to be more helpful by specifying a title attribute within our input element:

    <input
        type="text"
        name="username"
        placeholder="Username"
        pattern="[a-z]{1,15}"
        title="Username should only contain lowercase letters. e.g. john">

Now the title is included along with the default message:

Example Patterns

See http://html5pattern.com/

avatar brianteeman brianteeman - open - 6 Dec 2017
avatar joomla-cms-bot joomla-cms-bot - change - 6 Dec 2017
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 6 Dec 2017
avatar franz-wohlkoenig franz-wohlkoenig - change - 6 Dec 2017
Category Feature Request Fields
avatar dgt41
dgt41 - comment - 6 Dec 2017

@brianteeman about the validation message customisation: I've already included some code in the validation.js for that:

var mesgCont, message = el.getAttribute('data-validation-text');
if (label) {
mesgCont = label.querySelector('span.form-control-feedback')
}
if (!mesgCont) {
var elMsg = document.createElement('span');
elMsg.classList.add('form-control-feedback');
if (empty && empty === 'checkbox') {
elMsg.innerHTML = message ? message : Joomla.JText._('JLIB_FORM_FIELD_REQUIRED_CHECK');
} else if (empty && empty === 'value') {
elMsg.innerHTML = message ? message : Joomla.JText._('JLIB_FORM_FIELD_REQUIRED_VALUE');
} else {
elMsg.innerHTML = message ? message : Joomla.JText._('JLIB_FORM_FIELD_INVALID_VALUE');
}
if (label) {
label.appendChild(elMsg)
}

Just to mention that the proposal to drop the old validation script for a new fully HTML5 compliant was rejected (I think at the London sprint) mainly for B/C reasons

avatar brianteeman
brianteeman - comment - 6 Dec 2017

I am not talking about dropping the more advanced validation scripts.
I am talking about using the already partially supported pattern functionality

avatar C-Lodder
C-Lodder - comment - 6 Dec 2017

+1

Wouldn't addings a pattern to tel inputs be an absolute nightmare with multilingual sites?

avatar brianteeman
brianteeman - comment - 6 Dec 2017

That would be upto the site owner :) but we already have a validation rule for Tel formats that someone can use and it's completely useless :)

avatar franz-wohlkoenig franz-wohlkoenig - change - 6 Dec 2017
Status New Discussion
avatar dgt41
dgt41 - comment - 16 Dec 2017

@brianteeman if I understand correctly your request here there are 2 parts:

  • make sure that every input that doesn’t require custom validator uses the pattern
  • change my stupid implementation for the message to comply to HTML5 ( the title part)
avatar brianteeman
brianteeman - comment - 16 Dec 2017

Basically yes

avatar dgt41
dgt41 - comment - 16 Dec 2017

The js team can fix the js part but the forms xmls is a mountain of work and we’re way behind our schedule

avatar brianteeman
brianteeman - comment - 16 Dec 2017

If accepted the xml is something i can do
the js can probably be removed IF/WHEN this is implemented

avatar brianteeman
brianteeman - comment - 16 Dec 2017

NOTE we dont necessarily have to implement it in the core xml immediately that can wait for another time.
But we should be able to support it in com_fields and custom development

avatar brianteeman brianteeman - change - 25 Mar 2018
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2018-03-25 18:03:53
Closed_By brianteeman
avatar brianteeman
brianteeman - comment - 25 Mar 2018

Closed due to lack of interest

avatar brianteeman brianteeman - close - 25 Mar 2018

Add a Comment

Login with GitHub to post a comment