Solved

Javascript Numberic Validation

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html Table Looping (part 2) 5 39
Code Manager | Snippits 2 92
program arguments vs VM arguments 4 54
DW 2017 background color change 5 54
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

738 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