randy
randy

Reputation: 1877

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' : '[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() );
                            }                   
                   }
            ]
    }
]

Upvotes: 6

Views: 5901

Answers (1)

oleq
oleq

Reputation: 15895

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.' )
...

Upvotes: 12

Related Questions