3

The text box should accept onli decimal values in javascript. Not any other special characters. It should not accept "." more than once. For ex. it should not accept 6.....12 Can anybody help???

6 Answers 6

1

You can use regex:

function IsDecimal(str)
{
    mystring = str;
    if (mystring.match(/^\d+\.\d{2}$/ ) ) {
        alert("match");
    }
    else
    {
    alert("not a match");
    }
}

http://www.eggheadcafe.com/community/aspnet/3/81089/numaric-validation.aspx

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

Comments

0

You can use Regex.test method:

if (/\d+(\.\d{1,2})/.test(myTextboxValue)) //OK...

Comments

0

JQuery Mask plug in is the way to go! http://www.meiocodigo.com/projects/meiomask/#mm_demos

Comments

0

If you mean you do not want anything but an integer or a decimal to be typed into the field, you'll need to look at the value as each key is pressed. To catch pasted input, check it again onchange.

textbox.onkeyup=textbox.onchange=function(e){
    e= window.event? event.srcElement: e.target;
    var v= e.value;
    while(v && parseFloat(v)!= v) v= v.slice(0, -1);
    e.value= v;
}

Comments

0

probably you want to validate a form input before sending it to the server. Here is some example:

<html>
   <head>
      <title>Form Validation</title>
      <script type="text/javascript">
         function validate(){
            var field = document.getElementById("number");
            if(field.value.match(/^\d+(\.\d*)?$/)){
               return true;
            } else {
               alert("Not a number! : "+field.value);
               return false;
            }
         }
      </script>
   </head>
   <body>
      <form action="#" method="post" onsubmit="return validate();">
         <input type="text" id="number" width="15" /><br />
         <input type="submit" value="send" />
      </form>
   </body>
</html>

Comments

0

I just whipped this up. Useful?

<html>
<head>
<script type="text/javascript">
function validNum(theField) {
  val = theField.value;
  var flt = parseFloat(val);
  document.getElementById(theField.name+'Error').innerHTML=(val == "" || Number(val)==flt)?"":val + ' is not a valid (decimal) number';
}
window.onload=function(){
  validNum(document.getElementById('num'));
}
</script>
</head>
<body>
<form>
<input type="text" name="num" id="num"
onkeyup="return validNum(this)" /> <span id="numError"></span>
</form>
</body>
</html>

Comments

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.