0

I want to implement a regex functionality which is, A user can add only one decimal point in a textbox which I mention in my code. I tried like below but it is not working.

NOTE The regex is proper, its tried and tested

$(function () {
    $('input').on('#txtFiberActlength', function () {
        match = (/(\d{0,2})[^.]*((?:\.\d{0,4})?)/g).exec(this.value.replace(/[^\d.]/g, ''));
        this.value = match[1] + match[2];
    });
});

1 Answer 1

4

You forget to add event-handler inside your code,do like below:-

$(function () {
    $(document).on('keyup','#txtFiberActlength', function () {
        match = (/(\d{0,2})[^.]*((?:\.\d{0,4})?)/g).exec(this.value.replace(/[^\d.]/g, ''));
        this.value = match[1] + match[2];
    });
});

Sample example:-

$(function () {
  $(document).on('keyup','#txtFiberActlength, #txt2FiberActlength', function () {
      match = (/(\d{0,2})[^.]*((?:\.\d{0,4})?)/g).exec(this.value.replace(/[^\d.]/g, ''));
      this.value = match[1] + match[2];
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="txtFiberActlength">

<input type="text" id="txt2FiberActlength">

Sign up to request clarification or add additional context in comments.

3 Comments

ohh, my mistake.. can I also add mulitple id's here like this '#txtFiberActlength', '','', ...... ?
its not working on my side.. i am able to add multiple . this is my textbox. <input type="text" class="text-center" id="txtFiberActlength" onkeypress="return isNumberKey(event)" />
@VVVV you already have onkeypress on your input text-box that's why it's not working.Remove that and check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.