Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement layui regular expression validation

Shulou Source: shulou.com Published: 2022-06-02 13:38:29 09月18日 Update

This article focuses on "how to achieve layui regular expression verification", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement layui regular expression validation.

Preface

The regular expression of layui is done in the form form. So the first step is to add the specified form to your html.

Make sure that form.js exists in the referenced layui module.

The quick step refers to form.js to add the form tag, and sets the class attribute to layui-form. You cannot add lay-verify to the attribute to be verified to declare layui.form and listen for submitted button events. Quote js

The main purpose is to ensure the existence of form.js in lay.modules.

You can also quote form.js directly.

Add form tags

Set the properties to validate

Assign a value to lay-verify

The following attributes are included with the system:

Required (required)

Phone (mobile phone number)

Email (mailbox)

Url (website)

Number (digital)

Date (date)

Identity (ID card)

Custom value

If you don't have what you want, you can write it yourself, such as

Custom validation

Attributes validated by html tags

Custom rules for validation

Form.verify ({username: function (value, item) {/ / value: the value of the form, item: the form's DOM object if (! new RegExp ("^ [a-zA-Z0-9 _\ u4e00 -\ u9fa5\\ s] + $") .test (value)) {return 'username cannot have special characters' } if (/ (^\ _) | (\ _ + $) / .test (value)) {return 'usernames cannot be underlined\' _\';} if (/ ^\ d+\ d+\ d$/.test (value)) {return 'usernames cannot be all numeric' }} / / We support both the above functions and the two values of the following array: [regular matching, prompt text in case of mismatch], pass: [/ ^ [\ S] {6pr 12} $/, 'password must be 6 to 12 digits, and no spaces can appear]})

Instructions for using layui-form

1. You must render before select can be used.

Layui.use ('form', function () {var form = layui.form; / / only by performing this step will some form elements be automatically decorated successfully / /... / / however, if your HTML is dynamically generated, automatic rendering will fail / / so you need to perform the following methods to render form.render ();} in the appropriate place

Submit button

Confirm to place the order

Js custom js for verification and actions on submission

Layui.use ('form', function () {var form = layui.form; form.render (); form.verify ({payTotalAmount: [/ (^ [1-9]\ d (\.\ d {1Magne2})? $) | (^ 0 (\.\ d {1Magazine 2})? $) /,' Please enter the appropriate price']}); form.on ("submit (submit_button)", function () {onclickSearch ();}) At this point, I believe you have a deeper understanding of "how to implement layui regular expression validation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Validate regular attribute expression form user user name content button number array method label guarantee learning support appropriate deeper special underlined Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Xiaomi NVidia Apple