Solved

Javascript Numberic Validation

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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