Solved

Only allow user to input a whole number greater than zero

Posted on 2008-06-23
9
4,059 Views
Last Modified: 2008-06-24
I'm new to Javascript and I need help creating a function to do the following:

I want the user to be able to only enter whole numbers and  numbers greater than 0

This function will start when the submit button is pressed.

If an invalid number has been entered then the function should display an alert.

I've figured out how to check for a zero amount but not a decimal value


0
Comment
Question by:Carid
[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
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:ofkr
ID: 21852429
Try to use this simple code with regular expression.


<html><title>Number validation</title>
<script>
function validate(){
var NumberToValidate = document.main.age.value;
var valid = /^\d+$/.test(NumberToValidate);
alert (valid);
return valid;
}
</script>
<body>
<form name=main>
<input type=text name=age>
<input type=button value=submit name=submit onclick="return validate();"/>
</form>
</body></html>

Open in new window

0
 

Author Comment

by:Carid
ID: 21852480
Hi ofkr
Your solution does not work.

I am using Coldfusion and Internet Explorer is crashing with the error 'Unterminated string constant'


0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 3

Accepted Solution

by:
ofkr earned 500 total points
ID: 21852625
Sorry Carid,

Please remove "/" from this line and try again.
<input type=button value=submit name=submit onclick=/"return validate();">

Corrected:
<script type="text/javascript">
function validate(){
var NumberToValidate = document.main.age.value;
var valid = /^\d+$/.test(NumberToValidate);
alert (valid);
return valid;
}
</script>
....
<input type=button value=submit name=submit onclick="return validate();">

0
 

Author Comment

by:Carid
ID: 21852689
Yes that works for numbers input as decimals.


I don't want the users to be able to input zero amount as well.

Can it be changed to check for zero amounts as well
0
 
LVL 3

Expert Comment

by:ofkr
ID: 21852899
Replace with this one :

var valid =  /^([0-9]*[1-9][0-9]*)$/.test(NumberToValidate);

It should filter zero as well.
0
 

Author Comment

by:Carid
ID: 21861299
This is solution is great but I need to use it to validate a field in a recordset
I've been fiddling with the following function but nothing happens.
Not sure what is wrong
Any help would be appreciated


function validateQty()
{
	var theForm = document.frmCustOrderEntry;
    for(i=0; i < theForm.OrdQty.length; i++)
		var NumberToValidate = theForm.OrdQty[i].value;
		var valid =  /^([0-9]*[1-9][0-9]*)$/.test(NumberToValidate);
		{
		if (!valid)
			alert("Invalid Quantity!!\nThe Quantity cannot be Zero\n or a part of a Unit !");
			return false;
		}
		
return true;
}

Open in new window

0
 
LVL 3

Expert Comment

by:ofkr
ID: 21862028
As i don't have full of your code, Will it be like this? Check your matching braces.

function validateQty()
{
        var theForm = document.frmCustOrderEntry;
    for(i=0; i < theForm.OrdQty.length; i++){
                var NumberToValidate = theForm.OrdQty[i].value;
                var valid =  /^([0-9]*[1-9][0-9]*)$/.test(NumberToValidate);
               
                if (!valid){
                        alert("Invalid Quantity!!\nThe Quantity cannot be Zero\n or a part of a Unit !");
                        return false;
                }
     }          
return true;
}
0
 

Author Comment

by:Carid
ID: 21862410
Hi ofkr
Thank you for all your help
I managed to get it working by creating the following function
function validateQty()
{
      var theForm = document.frmCustOrderEntry;
   
      for(i=0; i < theForm.OrdQty.length; i++)
                  if(!/^[1-9]\d*$/.test(theForm.OrdQty[i].value))
                  {
                        alert("Invalid Quantity!!\nThe Quantity cannot be ZERO or a PART of a Unit.");
                        return false;
                  }
            
      return true;
}

Thanks again
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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