Q: KnockoutJS validation - Model variable regex not behaving as expected I have a knockout model build for a form and am using knockout for validation also. One particular field is giving me issues. The requirement for the textbox is it should allow any number of numerics [0-9], and optionally, up to 2 decimal places. My variable looks as follows
var debtIncomeRatio = ko.observable().extend({
    required: {
        onlyIf: function () {
            return (isQualified() == "Qualified" && (!nonReportable() && !isFinanced()));
        },
        message: "* Required"
    },
    pattern: {
        message: '* Number, no more than 2 decimal places',
        params: '^[0-9]+(\.[0-9]{1,2})?'
    }
});

My regex expression above, ^[0-9]+(\.[0-9]{1,2})?, should work from every forum and thread I have read. I have also tested it on sites like https://regex101.com/, which verifies that it should work. But in reality, when entering data on my form, it is allowing things such as 33,33, 33!33
or any special character in place of a decimal. Have I got a piece incorrect somewhere? Or does anybody know why it is it behaving this way?

A: Try using a regex literal instead of a string.   
pattern: {
    message: '* Number, no more than 2 decimal places',
    params: /^[0-9]+(\.[0-9]{1,2})?/
}


A: I'm not sure why, but I couldnt not get a regex literal to work either, but I found my error when using a regex string. Referring to this answer I found on S.O. Knockout-Validation Using Regular Expression to Validate a Phone Number, when using a regex string you must escape your backslashes, which I didn't know. 
