/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] do not allow certain characters in text box

Hi all,

I have this code that only allows a period and numbers to be put in the text box. The problem is that the message also appears if the text box is left blank. I don’t want the error to appear if the text box is left blank. Right now I have it on a Blur so that the error message appears when the person clicks outside of the text box. Any help would be great.

Thanks,
Cedric

<script language=javascript>
function checkNumeric(value)
{
var anum=/(^d+$)|(^d+.d+$)/
if (anum.test(value))
return true;
alert(“You can only input a period(.) and numbers(0-9) in this text box. Also, do not enter a dollar sign($) as this will automatically be added to your price.”);
return false;
}
</script>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@NihilisteOct 04.2010 — You could test if you have a value before doing the anum.test.

Something like this should do the job:

[CODE]
<script language=javascript>
function checkNumeric(value)
{
[B]if( !value ) { return true; }[/B]

var anum=/(^d+$)|(^d+.d+$)/
if( anum.test(value))
return true;
alert("You can only input a period(.) and numbers(0-9) in this text box. Also, do not enter a dollar sign($) as this will automatically be added to your price.");
return false;
}
</script>
[/CODE]
Copy linkTweet thisAlerts:
@cedric813authorOct 04.2010 — hi,

thanks for the reply...but nothing works now. Any further help would be greatly appreciated. Thanks
Copy linkTweet thisAlerts:
@cedric813authorOct 05.2010 — this is what's in the form:

<input name="pricenum" id="pricenum" type="text" size="5" maxlength="6" onBlur="return checkNumeric(this.pricenum.value)">
Copy linkTweet thisAlerts:
@cedric813authorOct 05.2010 — when i put this:

onSubmit="return checkNumeric(this.pricenum.value)"

within the <form> field...it works perfectly. But i don't want for it to wait until i submit the form before it gives the error. I want it to give the error when i select the next textbox so I would like to incorporate the above code in the actual textbox field like this:

onBlur="return checkNumeric(this.pricenum.value)"
Copy linkTweet thisAlerts:
@cedric813authorOct 05.2010 — thanks buddy! Works great!
×

Success!

Help @cedric813 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.20,
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,
)...