/    Sign up×
Community /Pin to ProfileBookmark

Trying to avoid the user can enter 0 values in a text box

How can I avoid the user enter 0 value in a text box? I tried it thus:

[code]
If (document.form_newOffer.textBoxField.value == 0) {
Alert(“You can’ t ener 0 values.”)
Return false; }
[/code]

But only works if the user enter 0 or 00 or 000 or 0000, etc.. but not if the user enter 0,0 or 00,00 or 0,000, etc,.. How can I do it so that the user can’ t enter all the values that are in fact 0?

Thank you

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Dec 16.2004 — <HTML>

<Head>

<Script Language=JavaScript>

function checkZero(isVal){

if (/[0]+/.test(isVal)){alert('No Zeros allowed')}
}


</Script>

</Head>

<Body>

<Form name='form_newOffer'>

<input type=text size=10 name='textBoxField'>

<input type=button value="Check It" onclick="checkZero(textBoxField.value)">

</form>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@erick30authorDec 16.2004 — Hi Warren,

I just were thinking in a similar solution. But your regular expression doesn' t allow any 0, so the user can' t enter 1000, or 0,05, etc..

Now the rule of this text box is:
<i>
</i>/^[d]{0,4}(,[d]{1,2})?$/


So, only numbers, for example: 0 or 0,05 or 250. With 4 digits as a maximum before coma, and 2 digits as a maximum after coma.

Then, I thought in write another rule to avoid 0 values, and I tested the same previous pattern but with only 0 numbers. I mean, if (thisRegExp.test(field) == true) then show the alert. Thus:
<i>
</i> var pat_cal_QA_mad2 = /^[0]{0,4}(,[0]{1,2})?$/
if (pat_cal_QA_mad2.test(cal_QA_mad) == true) {
alert("You can’ t enter 0 values.”)
document.form2_newOffer.calibre_QA_mad.focus()
return false; } <br/>
Copy linkTweet thisAlerts:
@vwphillipsDec 16.2004 — No reg expressions Im afraid but this will do it

[URL]http://homepage.ntlworld.com/vwphillips/VerifyTextBox/VerifyTextBox.htm[/URL]
Copy linkTweet thisAlerts:
@erick30authorDec 16.2004 — My previous code works fine, and it seems a good solution. Isn' t it?

Thank you to both
Copy linkTweet thisAlerts:
@JuuitchanDec 16.2004 — Fellows, I determined immediately that what this really is is an internationalization issue. erick30, it seems that in your land, a comma is used as a decimal point. Please this script on your computer, erick30:

<i>
</i>var foo=7/2;
document.write("Result A: "+foo+"&lt;br&gt;");
var bar="5.7";
document.write("Result B: "+parseFloat(bar)+"&lt;br&gt;");
document.write("Result C: "+(bar-0)+"&lt;br&gt;");
var baz="5,7";
document.write("Result D: "+parseFloat(baz)+"&lt;br&gt;");
document.write("Result E: "+(baz-0)+"&lt;br&gt;");


Please test this! I cannot do it for you myself! See that the results are:

Result A: 3.5 (not 3,5)

Result B: 5.7

Result C: 5.7

Result D: 5

Result E: 5

Is this exactly what you got?

If it is, the solution to your problem is simple. When you read any numbers, change the commas (,) to decimal points(.) before you do any math with those numbers! And when you get the answer, convert it to a string, and then replace any decimal points (.) with commas(,).

Wil someone else here please provide a string replace function to change all occurrences of "," to "."?
Copy linkTweet thisAlerts:
@erick30authorDec 17.2004 — My results are:

Result A: 3.5

Result B: 5.7

Result C: 5.7

Result D: 5

Result E: NaN


I think your solution is also very good, but I need a regular expression or a way to replace the "," with "." before doing the math operation: If (document.form_newOffer.textBoxField.value == 0) {..

And I don' t know how to do it.
Copy linkTweet thisAlerts:
@FangDec 17.2004 — <i>
</i>if(Number(document.myform.field.value.replace(/,/,'.'))) {
// process number
}
else {
// not number
}
Copy linkTweet thisAlerts:
@erick30authorDec 17.2004 — I tested your code, thus:

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;Script Language=JavaScript&gt;


function checkZero(isVal){

if (Number(document.form_newOffer.textBoxField.value.replace(/,/,'.'))) {

if (document.form_newOffer.textBoxField.value == 0) {
alert("The value can' t be 0.")
return false;
}
// process number
} else {
alert("This is not a number.")
return false;

// not number
}

}
&lt;/Script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;Form name="form_newOffer"&gt;
&lt;input type=text size=10 name="textBoxField"&gt;
&lt;input type=button value="Check It" onclick="checkZero(textBoxField.value)"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


And when I introduce 0 number, always appear the second alert (This is not a number). Why this happens?
Copy linkTweet thisAlerts:
@pj59Dec 17.2004 — Hello!

What about something like this?

function checkZero(isVal){

var no=document.form_newOffer.textBoxField.value

no=no.replace(/,/,'.')

no=parseFloat(no)

if (no == 0) {

alert("The value can' t be 0.")

return false;

}

else if (isNaN(no)){

alert("This is not a number.")

return false;

}

else {

alert("OK")

return true;

}

}

Regards PJ
Copy linkTweet thisAlerts:
@FangDec 17.2004 — Number attempts to convert a string to a number value, zero is false thus not a number.

Zero is not a valid input for you, so "This is not a number." is correct.
×

Success!

Help @erick30 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...