Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,415 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,843 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Help with numeric only input validation

 
Reply to this topicStart new topic

Help with numeric only input validation

jdidit
post 28 Jun, 2008 - 07:52 AM
Post #1


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 7

My code below is supposed to give an innerHTML msg if you enter anything other than a non-digit or decimal point. The problem I am having is I am getting a "oInputElem is null" error in firebug. It is pointing out this line as well as part of the error: if(!IsNumeric(oInputElem.value)) { (this is line 18 below). I am wracking my brain here... please take a look and help me out. Thanks.

CODE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <style type ='text/css'>
                  .error { color: red; }
        </style>

        <script type='text/javascript'>
            window.onload = initialize;
            function initialize() {
                var validate_isnumeric = document.getElementById("validate_isnumeric");
                validate_isnumeric.onclick = CheckNumeric();
            }

            function CheckNumeric() {
                var oErrorElem = document.getElementById("errorMessage");
                var oInputElem = document.getElementById("numOrText");
                if(!IsNumeric(oInputElem.value)) {
                    oErrorElem.innerHTML = "Invalid numeric input";
                    oErrorElem.className = "error";
                }else {
                    oErrorElem.innerHTML = "";
                    oErrorElem.className = "";
                }
            }

            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;
                     break;
                  }
               }
               return IsNumber;
               }
        </script>
    </head>

        <body>

                <form action="" onsubmit="return CheckNumeric();">
                   <div>
                       <input name="numOrText" type="text"><br>
                       <input type="submit" value="Validate">
                   </div>
                </form>
        </body>
</html>
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 28 Jun, 2008 - 08:30 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,184



Thanked 62 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Your input fields need to specify ids, not just names if you plan to use getElementById in javascript.

CODE

<input type="text" name="numOrText" id="numOrText">


Here we have specified the id with the name "numOrText" as well. Since you didn't have an ID on your input element, the getElementById() was returning null because it couldn't find it.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

jdidit
post 28 Jun, 2008 - 11:52 AM
Post #3


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 7

Hmmm... that cleared the " if(!IsNumeric(oInputElem.value)) {" error, now firebug is saying "oErrorElem is null" for the "oErrorElem.innerHTML = "";" statement. I don't understand why it would pass validation through the 1st innerHTML statement, and not the second. Is there someplace else I need to place an id statement to ID the "oErrorElem" var?:
CODE

if(!IsNumeric(oInputElem.value)) {
                    oErrorElem.innerHTML = "Invalid numeric input";
                    oErrorElem.className = "error";
                }else {
                    oErrorElem.innerHTML = "";
                    oErrorElem.className = "";
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/20/08 05:57AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month