?
Solved

Javascript Numberic Validation

Posted on 2013-01-22
4
Medium Priority
?
228 Views
Last Modified: 2013-02-06
the question is how can we do a java validation for a qty greater than 0 but Less than 1.  (Fraction, .1, .5, .25, etc.)

when using the onsubmit="return check(document.MA,document.MA.elements.length,'alertMA')">
and I have a text txtQty with [required="true" description="Quantity can't be zero" validationType="FLOAT" minValue="0.01"]

the system stop validating.  If I make the minValue="1" the validaion works.

Note there are many validation going on the form so putting a simple validate() does not work.
0
Comment
Question by:tnasca
[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 Comments
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 38808484
You should be able to validate txtQty by using:
if (document.getelementbyid('txtQty').value < 0 || document.getelementbyid('txtQty').value > 1)
{
     // Out of range
}

Open in new window


HINT: You should always make validation on server-side as well.
0
 

Author Comment

by:tnasca
ID: 38808513
Can you give more of an example of how this would work?
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38808594
Here is the simple html example..

<script>
function validate(){
if (document.getElementById('txtQty').value <= 0 || document.getElementById('txtQty').value > 1)
{
alert("out of range");
return false;
     // Out of range
}
return true;
}
</script>
<form >
<input type="text" value="" id="txtQty" name="txtQty">
<input type="submit" value="submit" name="submit" onclick="validate();return false;">
</form>

Open in new window


Since you need to validate zero also , i have changed Medo3337 code little bit
0
 
LVL 16

Accepted Solution

by:
Valeri earned 1500 total points
ID: 38809068
try this, it should work in all browsers:

function validateForm() {
qty = parseFloat(document.forms["myForm"]["qty"].value);
if (isNaN(qty)) return false;
if (qty <= 0 || qty > 1) return false;
return true;
}

<form name="myForm" action="someAction.jsp" onsubmit="return validateForm()" method="post">
Qty: <input type="text" name="qty">
<input type="submit" value="Submit">
</form>
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 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