Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2008, 02:49 AM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Textbox Validations using javascript

Hi friends i need to enter only numeric in textboxes.i tried this coed but it is not working.Can u mention wat is wrong in this code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <SCRIPT language=Javascript>

      function allownumbers(event)
        {
          var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }


   </SCRIPT>


Code-Behind File
protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("onkeypress", "javascript:return allownumbers(event);");
    }

 
Old January 18th, 2008, 02:59 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Try this for numbers only.

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   }

or this if you want to let them include decimals.

function ValidateForm(form)
{
   if(IsEmpty(form.account_number))
   {
      alert('You have not entered an account number')
      form.account_number.focus();
      return false;
   }


   if (!IsNumeric(form.account_number.value))
   {
      alert('Please enter only numbers or decimal points in the account field')
      form.account_number.focus();
      return false;
      }

return true;

}


 
Old January 18th, 2008, 03:12 PM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

<script language='javascript'>
validate()
{
     isInteger = /^[0-9]+$/;
     if (!isInteger .test(document.form.txtnumber.value))
     {
        alert(‘Enter valid number’);
    return false;
     }
     return true;
}
btnsubmit()
{
     if (validate())
     {
         document.form.submit();
     }
}
</script>
call btnsubmit function on any button click event, if function returns false then process further otherwise stop processing.


urt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript to validate a textbox abhishekkashyap27 Javascript 9 May 6th, 2008 02:41 AM
Javascript changes to Textbox Control wyokid ASP.NET 1.0 and 1.1 Professional 2 June 28th, 2006 01:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.