Pricing Blog

Regex Replace

  • _.__._.__.__._-1420317293010882570

    Ethan

    20 days ago

    Hi all, I'm trying to figure out how to perform the following javascript using formulas
    this.value = this.value.replace(/[^0-9+]/g, '')
    But I can't figure out what the correct method is, there is Matches and Replace All, but none of these achieve what I want without getting overly complex. I imagine I must be missing something. Any suggestions?
    1
  • jacobkofoed-1420320831472664639

    Jacob Kofoed

    20 days ago

  • _.__._.__.__._-1420328394045784064

    Ethan

    20 days ago

    This is what I originally tried. It doesn't work quite right as the user can still enter alpha characters, with it only changing once the user enters a numeric character again. Hence why you need to replace bad values, not just join the good ones together.

    My current solution is just to bind the JS snippet to the oninput attribute of the <input> field.
  • erikbeus-1420330038745894923

    Erik Beuschau

    20 days ago

    If you want to limit the user to only write numbers in an input, you could set the input's type attribute to number, or use the pattern attribute to limit which characters are allowed https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#pattern
  • _.__._.__.__._-1420671020414533632

    Ethan

    19 days ago

    Thanks, These also aren't quite right however. This is for a phone number, so its not truly numeric, meaning number Isn't a good fit. Pattern also only provides validation on submission, not true limiting as you can type numbers.

    My current solution seems to be the best fit for now, at least until formulas get a regex-replace method.