简体   繁体   中英

Sencha Touch simple model validation matcher

I am trying to validate a simple textfield that should accept either:

the numbers 0-100 OR the letters AF

To clarify, it is for a teacher grading assignments, so only the scores of 0-100 and AF are accepted (no special/non-alphanumeric characters etc).

I just can't seem to wrap my head around reg exp and the Sencha Touch matcher. I have seen the other posts but they are not quite applicable to my question. Thanks.

My model:

    Ext.define('App.model.EnterGradesModel',{
    extend: 'Ext.data.Model',
    alias: 'model.User',

    config: {
        fields: [
            {
                grade: 'grade'
            }
        ],
        validations: [
            {
                type: 'format',
                field: 'grade',
                matcher:    //This is where I need the help ----- ,
                message: 'Grade should be 0-100 or A-F'
            }
        ]
    }
});

Handler in my view:

        {
            xtype: 'button',
            width: 100,
            text: "Save",
            id: "saveMyGradeButton",
            action: 'saveMyGrade',
            handler : function() {
                var model = Ext.create('App.model.EnterGradesModel', this.up('formpanel').getValues());
                var errors = model.validate();
                console.log(model);

                if (errors.isValid()){
                    // Validation successful - fire grade event in controller
                    this.fireEvent('saveMyGrade', this);

                } else {
                    // Validation failed
                    var data="";
                    console.log(data);
                    errors.each(function (item, index, length) {
                        data = data + '| '+ item.getField() + ' - ' + item.getMessage() + ' |';
                    });
                    Ext.Msg.alert("oops.", data);
                }
            }
        }

This expression will validate one of the following conditions is true

  • value is 100
  • value is a double digit number 10-99
  • value is any single digit number 0-9
  • value is letters A thru F

^(?:100|[1-9]\\d|\\d|[AF])$

在此处输入图片说明

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