Author Topic: How do i apply formatting before validation?  (Read 2259 times)

gammax500

  • Pro Deluxe
  • Newbie
  • *
  • Posts: 16
    • View Profile
How do i apply formatting before validation?
« on: December 01, 2015, 01:37:06 am »
I have a column that holds US phone numbers that we want to have the format 999-999-9999. 

The column has validations, one of which is to check for hyphens.  I'd like to "help" the user by allowing numbers to be entered without hyphens, and then to validate.  How do i call a function to apply formatting before validation occurs?

The jQuery to add the hyphens to a number string would be something like:  $(this).val($(this).val().replace(/(\d{3})\-?(\d{3})\-?(\d{4})/,'$1-$2-$3'))


My column definition:

{ title: "Phone", width: '15%', dataType: "string", dataIndx : "phone" ,
                      validations: [
                         { type: 'nonEmpty', msg: "Required"},
                         { type: 'regexp', value: "^[0-9]{3}-[0-9]{3}-[0-9]{4}$", msg: "That does not appear to be a valid US phone number in the format 999-333-2222 (add the hyphens please!)" },
                      ]
                    },

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: How do i apply formatting before validation?
« Reply #1 on: December 01, 2015, 05:10:13 pm »
Since you want to add hyphens after value (without hyphens) is input by the user, the column.editor.getData callback can be used to alter the value.

Code: [Select]
editor: { getData: function( ui ){

var val = ui.$cell.find("input").val();
        return val.replace(/(\d{3})\-?(\d{3})\-?(\d{4})/,'$1-$2-$3')); //add hyphens to value entered by user. (according to regexp mentioned by you)
}},
« Last Edit: December 01, 2015, 05:16:16 pm by paramquery »