Solved

Javascript Numberic Validation

Posted on 2013-01-22
4
222 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
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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

10 Experts available now in Live!

Get 1:1 Help Now