skip to Main Content

Consider this form:

<form>
<input type="text" value="" placeholder="Enter no. of employee" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(..*)./g, '$1');" required/>
<input type="text" value="" placeholder="Enter Salary" required/>
<input type="submit"/>
</form>

I want a regular expression validation for Enter salary text feild with following:

  • The salary should be separated by commas like this format 1,00,000

  • It should not take any other input except numbers like in the case
    of "Enter no. of employee" field

2

Answers


  1. Chosen as BEST ANSWER

    Here is the solution for my problem:

    Indian currency:

    onkeyup="this.value = this.value.replace(/D/g, '').replace(/B(?=(?:(dd)+(d)(?!d))+(?!d))/g, ',');"
    

    US Currency:

    onkeyup="this.value = this.value.replace(/D/g, '').replace(/B(?=(d{3})+(?!d))/g, ',');"
    

    Working solution:

    <form>
        <input type="text" value="" placeholder="Enter no. of employee" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(..*)./g, '$1');" required/>
        <input type="text" value="" placeholder="Enter Salary" onkeyup="this.value = this.value.replace(/D/g, '').replace(/B(?=(?:(dd)+(d)(?!d))+(?!d))/g, ',');" placeholder="Enter Salary" required/>
        <input type="submit"/>
        </form>


  2. You can try this:

    • The salary should be separated by commas like this format 1,00,000

    • It should not take any other input except numbers like in the case of
      "Enter no. of employee" field.

    <form>
      <input type="text" value="" placeholder="Enter no. of employee" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(..*)./g, '$1');" required/>
      <input type="text" value="" onkeyup="this.value = this.value.replace(/D/g, '').replace(/B(?=(d{3})+(?!d))/g, ',');" placeholder="Enter Salary" required/>
      <input type="submit" />
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search