Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Only allow user to input a whole number greater than zero

Posted on 2008-06-23
9
Medium Priority
?
4,377 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
  • 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
Technology Partners: 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 3

Accepted Solution

by:
ofkr earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

927 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