Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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
  #1 (permalink)  
Old June 7th, 2007, 05:00 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input Validation

What are the various ways to allow only numeric entry in a Text box using Javascript.

  #2 (permalink)  
Old June 7th, 2007, 05:20 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

There can be different ways to block non-numeric entries, some widely used can be
a) regular expressions
b) check for each character and match for numeric value
c) Determine and block the keys based on keypress event.

You can use the below two examples to test the above (a & c points), copy and paste the code below in a notepad, and save it as .htm. Do let me know if you find this post useful.

Test for Numeral using regular expression
------------------------------------------


<HTML><HEAD>
<TITLE>Test for Numeral using regular expression</TITLE>

<SCRIPT>
        function isValidNumber(str)
        {
            var sValue;
            sValue = str;
            var decimalRE = "^(\\+|-)?[0-9][0-9]*(\\.[0-9]*)?$";


            if (!(sValue.match(decimalRE) != null))
            {
                alert("Only valid numbers are allowed.");
                return false;
            }
            return true;
        }
    </SCRIPT>
</HEAD>
<BODY>
Please
enter a value: <INPUT id=txt><BR><INPUT id=btn onclick="return

isValidNumber(document.getElementById('txt').value )" type=button value="Check

Numeric">
</BODY>
</HTML>




Test for Numeral using keycodes on keypress event
-------------------------------------------------


<HTML><HEAD>
<TITLE>Validate numerals on keypres</TITLE>
<SCRIPT>
        function NumValidateDecimal(obj)
        {
            if((window.event.keyCode>=48 &&

window.event.keyCode<=57)|| window.event.keyCode==46 )
            {

                if(window.event.keyCode==46)
                {
                    var strVal=String(obj.value);
                    var myRegExp=/\./;
                    var resIndex=strVal.search(myRegExp);

                    if(resIndex!=-1)
                        return false;
                }

                if(window.event.keyCode==45)
                {
                    var strVal=String(obj.value);
                    var myRegExp=/\-/;
                    var resIndex=strVal.search(myRegExp);

                    if(resIndex==-1)
                        obj.value="-"+obj.value;

                    return false;
                }

            }
            else
                return false;
        }
    </SCRIPT>
</HEAD>
<BODY>
Please
enter a value: <INPUT onkeypress="return NumValidateDecimal(this)" id=txt>

</BODY></HTML>


Regards
Mike

Don't expect too much, too soon.
  #3 (permalink)  
Old June 7th, 2007, 07:35 AM
Friend of Wrox
 
Join Date: Aug 2006
Location: Amman, , Jordan.
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

to use regularExpression.. use this
[-]{0,1}\d*\.*\d+


Nothing is impossible. The impossible only takes longer. "Digital Fortress, Dan Brown"
  #4 (permalink)  
Old June 12th, 2007, 01:48 AM
Friend of Wrox
 
Join Date: Aug 2006
Location: Bangalore, Karnataka, India.
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default

i think regular expression [0-9]* also work for numeric entries only.


thanks......
  #5 (permalink)  
Old June 12th, 2007, 03:29 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, but check the expression provided by me more carefully, you will some more characters allowed within it....

Regards
Mike

Don't expect too much, too soon.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation to input fields amol_0008 XML 1 May 30th, 2007 04:36 AM
Input validation in JFormattedTextField harini19 Java Basics 2 November 14th, 2006 01:50 AM
Input validation shs BOOK: Beginning ASP.NET 1.0 14 January 6th, 2005 01:03 PM
Input Validation Maxood C++ Programming 11 August 6th, 2004 02:55 PM
Validation of TextBox Input bpgadhia ASP.NET 1.0 and 1.1 Professional 1 March 16th, 2004 05:07 PM





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