?
Solved

How can I incorporate a statement into this code to check if a text number was entered?

Posted on 2004-04-02
16
Medium Priority
?
339 Views
Last Modified: 2008-02-01
Hi Experts,

I have the following code I got from an Expert. How can I incorporate a code to check if unitprice is a numeric value?

THANKS

<SCRIPT LANGUAGE="JavaScript">
// form validation - make sure first name and
// last name fields are filled in by user
function validate()
{
x=document.myForm

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
else // fields populated
 {
 return true
 }
}
</script>
0
Comment
Question by:indyng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +2
16 Comments
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10740840
<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>
Such a function can be used as follows to check a field and return an error message if the contents are not numeric:
   if (document.frmUser.afield.value.length == 0)
      {
      alert("Please enter a value.");
      }
   else if (chkNumeric(document.frmUser.afield.value) == false)
      {
      alert("Please check - non numeric value!");
      }
 

 
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10740845
Just stick a test for IsNumeric(input) somewhere then in your code and responsd accordingly...
0
 
LVL 12

Expert Comment

by:monosodiumg
ID: 10740852
function validate()
{
x=document.myForm

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
else if isNaN(input) {
    alert(input + " is not a number.");
} else {
 return true
 }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Shailesh15
ID: 10740868
There is a sample code with function to test numeric value with various options at....

http://www.shiningstar.net/articles/articles/javascript/checkNumeric.asp?ID=ROLLA
0
 
LVL 1

Author Comment

by:indyng
ID: 10741035
I tried the following, but it doesn't work.
Can someone please correct it?
Thanks

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
if (chkNumeric(input) == false)
 {
 alert(input + " is not a number.");
 }
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10741125
Should be:

 Home All Topics Web Development Web Languages ASP Viewing a Question


 Active Server Pages (ASP) Solutions and Articles
   
 Question Title: How can I incorporate a statement into this code to check if a text number was entered?
Author: indyng
Points: 500
Date: 04/02/2004 03:16PM IST
 


Hi Experts,

I have the following code I got from an Expert. How can I incorporate a code to check if unitprice is a numeric value?

THANKS

<SCRIPT LANGUAGE="JavaScript">
// form validation - make sure first name and
// last name fields are filled in by user
function validate()
{
x=document.myForm

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
else // fields populated
 {
 return true
 }
}
</script>

 
 Send to a Friend     Printer Friendly  
 
   
   
 Comment from Slimshaneey
Date: 04/02/2004 03:28PM IST
 Your Comment  


<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>
Such a function can be used as follows to check a field and return an error message if the contents are not numeric:
   if (document.frmUser.afield.value.length == 0)
      {
      alert("Please enter a value.");
      }
   else if (chkNumeric(document.frmUser.afield.value) == false)
      {
      alert("Please check - non numeric value!");
      }
 

 
 
Comment from Slimshaneey
Date: 04/02/2004 03:29PM IST
 Your Comment  


Just stick a test for IsNumeric(input) somewhere then in your code and responsd accordingly...
 
Comment from monosodiumg
Date: 04/02/2004 03:29PM IST
 Comment  


function validate()
{
x=document.myForm

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
else if isNaN(input) {
    alert(input + " is not a number.");
} else {
 return true
 }
}
 
Comment from Shailesh15
Date: 04/02/2004 03:31PM IST
 Comment  


There is a sample code with function to test numeric value with various options at....

http://www.shiningstar.net/articles/articles/javascript/checkNumeric.asp?ID=ROLLA
 
Comment from indyng
Date: 04/02/2004 03:53PM IST
 Author Comment  


I tried the following, but it doesn't work.
Can someone please correct it?
Thanks

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
if (IsNumeric(input) == false)
 {
 
 alert(input + " is not a number.");
 }
 
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10741140
OOps!!!! Not sure what happened there!! Here it is again!

The code should be like this::
input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
if (IsNumeric(input) == false) //chkNumeric incorrect as the func name is IsNumeric
 {
 
 alert(input + " is not a number.");
 }
0
 
LVL 1

Author Comment

by:indyng
ID: 10741229
I tried the following:

input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }

if (IsNumeric(input) == false)
 {
 
 alert(input + " is not a number.")
 }

Does not work
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10741249
Have you put the function in the code? ie should look like this:
You are also missing a few semi-colons. Ive added them.

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;
   }

input=x.unitprice.value;
if (input.length<1)
 {
 alert("Please enter item price.");
 return false;
 }

if (IsNumeric(input) == false)
 {
  alert(input + " is not a number.");
return false;
 }


0
 
LVL 1

Author Comment

by:indyng
ID: 10741308
I require a Java Scipt for the IsNumeric function
0
 
LVL 12

Accepted Solution

by:
monosodiumg earned 2000 total points
ID: 10741310
input=x.unitprice.value
if (input.length<1)
 {
 alert("Please enter item price.")
 return false
 }
else if (isNaN(input))
 {
 alert(input + " is not a number.")
 }

0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10741337
Thats what Ive posted, a function called IsNumeric... will accept any number and return false if there is any character that is not a point or a number. If you use the code in my last poast it should work fine...
0
 
LVL 10

Expert Comment

by:D_M_D
ID: 10741361
Try this...Just save all the code to a new page

<html><head><title></title>
<script language="JavaScript">
<!--

function validate(objForm) {
     var txtVal = objForm.unitprice.value;
     if (txtVal ==  ""){
           alert("Please enter item price.")
           objForm.unitprice.focus();
           return false;
      }else{
           if (CheckNumeric(txtVal)){
                return true;
           }else{
       alert ("Please enter only numeric digits");
       objForm.MyForm.unitprice.focus();
       return false;
            }
       }
}

function CheckNumeric(theValue){
      var ValidNumbers = "0123456789";
    var chrN = "";
    var chrV = "";
    var found = 0;

    for(var i = 0; i < theValue.length; i++){
          chrN = theValue.charAt(i);

           for (var j = 0; j < ValidNumbers.length; j++){
                 chrV = ValidNumbers.charAt(j);

                 if (chrN == chrV){
                      found = found + 1;
                 }
           }
     }

     if (found == theValue.length){
          return true;
     }else{
         return false;
     }
}
-->
</script></head><body>
<form name="MyForm">
  <input type="text" name="unitprice"><br>
<input type="reset" name="reset" value="Reset">
 <input type="button" name="button1" value="Add" onclick="JavaScript:validate(document.MyForm);">
</form>
<body></html>

--------
D_M_D
0
 
LVL 10

Expert Comment

by:Shailesh15
ID: 10741362
I just tried Slimshaneey solution . It works fine.

copy the solution full except for  comments. It will/should/must work!

If you are still having problem better look at other html code.
Make sure that yourform is named 'myform' & textbox 'unitprice' (with lowercase)
0
 
LVL 1

Author Comment

by:indyng
ID: 10741372
Slimshaneey

Are you sure IsNumeric works for Java Script?
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 10741380
Certain... Ive used it one other sites. I think you might have an error somewhere else in the code...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question