Solved

Only allow user to input a whole number greater than zero

Posted on 2008-06-23
9
3,968 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 2

Expert Comment

by:ayus80
ID: 21852354
0
 
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

730 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