Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ckeditor plugin - validating a text field

Tags:

ckeditor

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' : '[email protected]',
                            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() );
                            }                   
                   }
            ]
    }
]
like image 373
randy Avatar asked Nov 27 '12 22:11

randy


1 Answers

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.' )
...
like image 153
oleq Avatar answered Oct 24 '22 06:10

oleq