[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Javascript Numberic Validation

Posted on 2013-01-22
4
Medium Priority
?
229 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

649 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