Phone regular expression

Phone regular expression

anithanokkuanithanokku Posts: 3Questions: 1Answers: 0

I want to do a phone validation before inserting data to database., how can i use a regular expression, do data tables support an inbuilt feature to support phone / zip code validations. I could only set the required field validation. Following is the code

editor.on('preSubmit', function (e, o, action) {

    var BusinessName = editor.field('BusinessName');       
    var PostalCode = editor.field('PostalCode');
    var iPhone = editor.field('iPhone');

    if (!BusinessName.val()) {
        BusinessName.error('BusinessName must be given');
    }       
    if (!PostalCode.val()) {
        PostalCode.error('PostalCode must be given');
    }
    if (!iPhone.val()) {
        iPhone.error('A phone number must be given');
    }

    if (this.inError()) {
        return false;
    }
});

Is there a property that could be set in ipOpts for phone field validation and zip field validation.

Thanks.

Replies

  • allanallan Posts: 61,887Questions: 1Answers: 10,141 Site admin

    Hi,

    Editor focuses on validation on the server-side since validation must be done there anyway. Under the assumption that the Ajax query is fairly fast to process, generally I would suggest doing the validation at the server only rather than duplicating the effort.

    At the server-side you can use the validation methods. There isn't a built in validation method for telephone numbers since the format of them can vary significantly throughout the world. As such you'd need to use a custom validation method as described on that page.

    If you need to do it on the client-side, you would use a regular expression in Javascript as normal on the phone number field - similar to the validation you have above.

    Regards,
    Allan

  • anithanokkuanithanokku Posts: 3Questions: 1Answers: 0
    edited April 2016

    Hi Allan,

    Thanks for the link .. I have already tried the .NET validation as mentioned in https://editor.datatables.net/manual/net/validation b
    Following is the way I am using it but it still doesn't validate

    Code:

    editor = new $.fn.dataTable.Editor({
            ajax: {            
                edit: {
                    type: 'POST',
                    url: '../RequiredFieldVailator'
                    data: function (d) {
                        return data;
                    }, error: function (result) {                  
                          alert (result);
                    }
                }
            },
            table: "#example",
            fields: [{
                label: "Business Name:",
                name: "BusinessName",
                validator: "Required"            
            }, {
                label: "Postal Code:",
                name: "PostalCode",
                validator: "Validate::numeric"
            }, {
                label: "Phone:",
                name: "iPhone"
            }
            ]
    
        });
    

    Thanks.

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 61,887Questions: 1Answers: 10,141 Site admin

    There is no validator option on the client-side. As I tried to explain above, Editor focuses on server-side validation. As the documentation shows shows, the validation is applied in the C# code, not Javascript as you have tried to do above.

    Allan

This discussion has been closed.