Solved

Only allow user to input a whole number greater than zero

Posted on 2008-06-23
9
3,811 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now