Solved

Only allow user to input a whole number greater than zero

Posted on 2008-06-23
9
3,851 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

863 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

19 Experts available now in Live!

Get 1:1 Help Now