Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

replacing characters in an input with regex and javascript

So I've been trying to rap my head around this and searching high and low to find an answer.

I have an input id of date that should only allow a YYYYMM input. I was instructed to make the input sanitize the value on 'keyup '

so I have

    $('body').on("keyup", '#date', function(){
        var value = $(this).val();
    value = value.replace(/\D/g,'');
    $(this).val(value);
    });

the input have a maxlength of 6 so it works

I have two other inputs though one "rate" and one "minimum"

rate should allow only numbers and a single decimal with 1 digit before the decimal and 6 after the decimal like #.######

if as they are typing they have ## before they hit . it should strip the last number out so only one number goes before the decimal. and upon submitting if the numbers after the decimal are less than 6 the remaining spaces should be populated with zeros so #.## should be #.##0000

for minimum its a little different. there can be 3 numbers before the decimal but only require 1 and there must be 2 digits after the decimal so ###.## or ##.## or #.## is correct and .## is not nor is ##.# or .# if there are no # before the . 0 should be prepended and if there are less than 2 # after the decimal 0 should be appended.

This task has been painstakingly argued and I can't get around having to do it in a combination of keyup and submit or keyup and off-focus.

Which challenge/task is it?

its not, its side project stuff. but I got it. I decided to handle it in PHP before it fires off SQL