简体   繁体   中英

Ckeditor plugin - validating a text field

I am creating plugin I have this piece of code below:

What i am trying to do is make sure the email address they enter is valid. Just not sure how to stop the onOK if the email address is not valid.

Thanks

This is a code snippet of the plugin

contents : [
    {
            id : 'info',
            label : editor.lang.form.title,
            title : editor.lang.form.title,
            elements : [
                    {
                            id : 'destEmail',
                            type : 'text',
                            label : 'Email form results to:',
                            'default' : 'randy@me.com',
                            required : true,
                            accessKey : 'T',
                            commit : function( element )
                            {
                                var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
                                if (this.getValue().search(emailRegEx) == -1) {
                                    alert("Please enter a valid email address.");
                                    return false;
                                }
                                element.setAttribute('id', this.getValue() );
                            }                   
                   }
            ]
    }
]

Please take a look on official sample and validate property. You can write your own validation method at this point.

You can also use one of the available (still not documented in API). You probably want to do something like this (CKEditor 4):

...
validate: CKEDITOR.dialog.validate.regex( /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, "Please enter a valid email address." );
...

It is also possible to combine existing validators and/or write custom validators:

function customValidator( x, msg ) {
    return function() {
        var value = this.getValue(),
            pass = !!( CKEDITOR.dialog.validate.integer()( value ) && value < x );

        if ( !pass ) {
            return msg;
        }
    };
}

...   
validate: customValidator( 5, 'Error message when larger than 5.' )
...

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