Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
344 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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