Visual Studio .NET - CustomValidator to validate numeric value

Asked By Partha Chakraborty on 30-Jan-07 06:44 AM

Hi,

I am trying to check the value in a textbox and validate the textbox whether it is containing a valid number or not (no float). I am using CustomValidator. My javascript

for the validator is:

 

<script language="javascript" type="text/jscript">

function Checknum1(sender, args)

{

    var valid = "0123456789"

    var ph = args.value;

   

    for (var i=0; i < args.value.length; i++)

    {

        var temp = args.substring(i, i+1);

        alert ("tmmp is--"+temp+"--");

        if (valid.indexOf(temp) == "-1")

        {

            alert("Invalid characters entered.Please enter digits only.");

            return false;

        }

    }

    return true;

}

 

</script>

 

But the statement "args.value.length" is not working because "length" is not a valid

property in this case. But actually length is a valid in pure javascript.

Please help.

Partha

use CompareValidator control and set Operator="DataTypeCheck" and Type="Integer" then only it allow integer only integer

K Pravin Kumar Reddy replied to Partha Chakraborty on 30-Jan-07 10:47 AM
 use CompareValidator  control and set Operator="DataTypeCheck" and Type="Integer" then only it allow integer only integer

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox3"

ErrorMessage="Enter Valid Number" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>

Re :: Javascript Numeric Validation - Shailendrasinh Parmar replied to Partha Chakraborty on 28-Nov-08 04:33 AM

See the following function for checking the numeric value in javascript

<SCRIPT language="JavaScript">
  <!--

function IsNumeric(strString)
   //  check for valid numeric strings 
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

  // -->
</SCRIPT>

Hope this helps.

Re :: Javascript Numeric Validation - Shailendrasinh Parmar replied to Partha Chakraborty on 28-Nov-08 04:34 AM

See the following articles for checking numeric values

http://www.codetoad.com/javascript/isnumeric.asp

http://javascript.internet.com/forms/validate-numeric-only.html

Hope this helps.