0

i am trying to put validation on a textbox onkeyup. Textbox should contain only 5 digit value and after decimal only upto 4 decimal places. eg,12345 ,12345.2345 if user enter value other than regex then the texbox should become blank and i want it to be done in function and this function should be generic so that any other can use this function .Aspx

<input type="number" id='inpSurfIndN' value='' runat="server" onkeyup="isFloatNumber(this.value)" /> 

Script function

<script type="text/javascript">
function isFloatNumber(value) {
 var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                value = "";
                return false;
            }
            return true;
        }
</script>
3
  • Did you try $('#inpSurfIndN').val("") instead of value = ""? Commented Sep 20, 2018 at 5:25
  • @JitendraRangpariya actually i want it to be generalised i mean if there will be five different textbox and all have same function so i want it to make generic.no need to write different function for different textbox keyup event. Commented Sep 20, 2018 at 5:28
  • you can pass 'event' parameter to isFloatNumber function and then when it executes function isFloatNumber, eve argument contains the element on which the function is called. Refer to my answer in detail Commented Sep 20, 2018 at 5:35

5 Answers 5

2

function isFloatNumber(elem) {
 var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(elem.value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                elem.value = "";
                return false;
            }
            return true;
        }
<input type="number" id='inpSurfIndN' value='' runat="server" onkeyup="isFloatNumber(this)" /> 
<input type="number" id='inpSurfIndN1' value='' runat="server" onkeyup="isFloatNumber(this)" /> 
<input type="number" id='inpSurfIndN2' value='' runat="server" onkeyup="isFloatNumber(this)" /> 

You can use above snippet which will work for n numbers of inputs.

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

Comments

0

Updating value = ""; doesn't update the UI element. You should access the UI Element object by passing this and update the value like this.value = " " else you should use the selectors like document.getElementbyId() to access those object like document.getElementbyId('inpSurfIndN').value = ""

Comments

0

One of way you can use below logic,

function isFloatNumber(obj) {
  var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
  var regmatch = regex.test(obj.value);
  if (regmatch == null || regmatch == false) {
    alert("Please fil correct expression");
    obj.value = "";
    return false;
  }
  return true;
}
<input type="number" id='inpSurfIndN' value='' runat="server" onkeyup="isFloatNumber(this)" />

Comments

0

var n = document.getElementById("numPeople"),
    r = document.getElementById("result");

n.addEventListener("keyup", function(e) {

    var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(n.value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                n.value='';
                return false;
            }
}, false);
<input id="numPeople" type="number" min="0" value="" placeholder="Pick a number" />

Comments

0

You cannot access the value variable which is passed as a parameter, it doesnt reference to the value of the input box Instead you can access the element and change the value like below:

 function isFloatNumber(eve) {
            var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                var elem = eve.currentTarget;                 
                elem.value = "";
                return false;
            }
            return true;
        }

1 Comment

I think you need meant eve.currentTarget or function isFloatNumber(e).

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.