Solved

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

Posted on 2004-04-02
16
329 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Attaching a file to SMTP in Classic ASP 1 42
INNER JOIN ? 8 50
File download script VBSCRIPT fail on download PPT files 2 59
Html value of radio 14 27
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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