This is a migrated thread and some comments may be shown as answers.

[Solved] Entering a decimal point as first character in a number field.

1 Answer 17 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Igor
Top achievements
Rank 1
Igor asked on 29 Jun 2012, 09:09 PM
Hello Telerik,

I would like to share a small tweak that I have created, that you might find useful to include in the next release. 
I have created a number validator that would accept a decimal as the first character via a modification to the regular expression. This has been working great, until I added a grid to the page and started inserting data. Suddenly I would start getting the 'The value you entered isn't a number' from my numeric text fields that had values with the leading decimal point character. This took some time to identify but the telerik.grid.editing.js has its own version of unobtrusive validation, and there it defines the 'number' validator. This validator sadly doesn't allow leading decimal points.

Original regex from telerik.grid.editing.js: /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/
Change regex to: /^-?(?:\d*|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/

The change will allow for zero or more digits in front of the decimal point.

Here is the tweaked validator:

$.validator.addMethod('number', function (value, element) {
    var groupSize = $t.cultureInfo.numericgroupsize;
    if (groupSize) {
        var builder = new $t.stringBuilder();
 
        builder.cat('^-?(?:\\d*|\\d{1,')
                    .cat(groupSize)
                    .cat('}(?:')
                    .cat($t.cultureInfo.numericgroupseparator)
                    .cat('\\d{')
                    .cat(groupSize)
                    .cat('})+)(?:\\')
                    .cat($t.cultureInfo.numericdecimalseparator)
                    .cat('\\d+)?$');
       return this.optional(element) || (builder && new RegExp(builder.string()).test(value));
    }
    return this.optional(element) || /^-?(?:\d*|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
});

Hope this helps.

Thanks,
Igor 

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 04 Jul 2012, 02:51 PM
Hi Igor,

Thank you for the suggestion we will discuss it with the Dev team. Currently the validator logic is the same as the one used by jQuery:
http://docs.jquery.com/Plugins/Validation/Methods/number 


Kind Regards,
Petur Subev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
Grid
Asked by
Igor
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or