Solved

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

Posted on 2004-04-02
16
323 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now