简体   繁体   中英

how to validate p,li,div elements text

I want to validate the p,li,div element texts. examples, I have an p tag with the class name "date". then the user can insert only a date.if they inserting text I want to display alert to the user in CKEDITOR .

Is it possible to in ckeditor without using any input fields <p class="date"></p>

<p class="date">31/07/2018</p>

<p class="date">a</p> I need to display error or alert.

how can i do without onchange events. is there have any default funtionality

You can get elements by class in jQuery. Then the validation process is straightforward

function isDate(value) {
    var dateReg = /^\d{2}([./-])\d{2}\1\d{4}$/
    return value.match(dateReg)
}

$("p.date").map(function() {
    if(!isDate(this.innerHTML)) {
        //alert("...");
    }
});

As for the part - when to run these validation checks, you can call them on DOM updates like, insertion, deletion etc in the application layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM