How to add support for HTML5 input types? I want to do some simple client side validation.

How to add support for HTML5 input types? I want to do some simple client side validation.

sockbotsockbot Posts: 2Questions: 1Answers: 0

The input field types supported by Editor (https://editor.datatables.net/reference/field/) seems to be a subset of the input field types supported in HTML5 (https://www.w3schools.com/tags/att_input_type.asp).

I'd like to be able to use the email and tel input types as a simple way to do client side validation. Is there a way that I can change the way Editor creates the input fields so it supports the type "email" and "tel"?

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Editor's PHP libraries have a validator for email.
    https://editor.datatables.net/manual/php/validation#Strings

    I don't see the point of HTML5's "tel" input type, as there is no universal standard for entering telephone numbers and it is not supported by most browsers.

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    Use the attr option of the default text field type to alter the type attribute - e.g.:

    {
      label: 'E-mail:',
      name: 'email',
      attr: {
        type: 'email'
      }
    }
    

    Allan

  • sockbotsockbot Posts: 2Questions: 1Answers: 0
    edited September 2018

    I tried this, but the email and telephone fields are still not being validated. Editor is not showing any error in the console so I'm not sure how to diagnose this.

      {
        label: "Crew Owner Email:",
        name: 'crewowner.email',
        attr: {
          type: 'email',
        },
      },
      {
        label: "Crew Owner Phone Number:",
        name: 'crewowner.phonenumber',
        attr: {
          type: 'tel',
        },
      },
    
  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    Have you used the :valid pseudo selector to highlight the field if it is invalid? See the MDN documentation for an over view on this.

    The form won't just automatically not submit if an invalid e-mail address is entered - you would need to listen on preSubmit and check if the fields are valid or not.

    Keep in mind also that client-side validation is trivial to bypass, which is why the Editor examples focus on server-side validation.

    Generally I would recommend using HTML5 input types with styling as a validation hint to the end user only.

    Allan

This discussion has been closed.