Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I stop clients from placing negative quantities on a form

Status
Not open for further replies.
Jul 13, 2001
180
US
How do I stop them from entering neg quantities (-2..etc) so that the cost doesn't become a negative price?


Unfortunately, The help I received from the Javascript forum did not help. Here's the thread thread216-229562.

Scoured the web for answers, but to no avail. I have a sample of the very chock full code ( and forgive , a bit amateur) with the thread above, where I would hope to add the stop of using negative quantities.

Any help would be so greatly appreciated.
 
You can stop it at client side itself. This question has to answered at JavaScript forum ... anyway if u have not got answer there then try the following:

suppose following is the input box for u ..

<INPUT type=text name=Price id=txtPrice>

wirte a javascript function to check whether user has entered a negtive values as:

function isValidPrice()
{
var Price = (document.forms[0].txtPrice.value).replace(/(^\s*)|(\s*$)/g, &quot;&quot;)
//.replace is to trim the text entered in the textbox

if (Price!=&quot;&quot;)
{
if (isNaN(Price))
{
alert(&quot;Please Enter Valid value for the filed.&quot;)
return false;

}
else
{
if (parseFloat(Price,10)<0)
{
alert(&quot;Please enter price greater than 0.&quot;)
return false;
}
}

return true;
}

}
The above function will return false if user has entered invalid price and true if user has not entered any price or entered valid price.


I think U got the point. U can do the modifications according to u'r requirements. Call this function where validation is required to be performed. (Call it on submit).


Hope this helps ...

srinu...
 
Hey guys,

You can solve this problem without using Javascript at all, and then you don't need to worry about which browser is being used, or whether or not JS is disabled.

You can simply use ASP to do the validation. The form redirects to the page it is contained in, then the code at the top of your ASP script does the validation. When the validation turns up no errors, you redirect, otherwise you display errors.

ie..

<!-- test.asp -->
<%
Dim bValidPage
Dim iNumber

iNumber = Request(&quot;iNumber&quot;)

'Do validation here....

bValidPage = True

If iNumber < 0 then
Response.Write &quot;Hey! Can't use negative numbers!!&quot;
bValidPage = False
End if

'More validation....
'etc

If bValidPage = True then
Response.Redirect &quot;nextpage.asp&quot;
End if
%>

<HTML> etc etc

<FORM ACTION=&quot;test.asp&quot; METHOD=&quot;POST&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;iNumber&quot;
VALUE=&quot;<%=Request(&quot;iNumber&quot;)%>&quot;>
<INPUT TYPE=&quot;submit&quot;>
</FORM>

</HTML>

<!-- End of test.asp -->


This example will not only do all validation in ASP, it will also remember the values that were entered in the form as it displays the error message. This stops the users having to enter everything in again when they type invalid data. ie the VALUE=&quot;<%=Request(&quot;iNumber&quot;)%>&quot; section of the text control.


If this has helped please vote!! :)
Brett Birkett B.Comp
Systems Analyst
 
yes Bbirkett what u said is correct .. it is the way in which I maintain the data if some addition or database operation fails ...But it is again a serverside validation ... Do user realyy have to wait for few minutes to know that he had entered wrong values??? and he has to enter them again?
So disirable choice will be both sides validation ....
use server as well as client side vlidations .. If user has disabled the javascript then he has to pay the price by waiting and letting know by the server that he had enetered wrong values... he had opted for it ...

Cheers!

srinu...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top