/    Sign up×
Community /Pin to ProfileBookmark

validation for 0%~100%

Can some one please tell me how to validate input value for
0%~100% inclusive. Need user to enter value so that Values should range from 0%~100% inclusive.

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@ananiasJul 15.2006 — But there are different approaches, depending on how exactly you want the form to behave. Do you want the user to be left alone until the submit is attempted ([color=tan]<form action= ... [color=red]onsubmit='[color=green][some javascript code][/color]'[/color] ... >[/color]) or to notify them as they try to leave the field ([color=tan]<input type="text" ... [color=red]onchange='[color=green][some javascript code][/color]'[/color] ... >[/color]), or after they have typed the first character that renders the field invalid?([color=tan]<input type="text" ... [color=red]onkeydown='[color=green][some javascript code][/color]'[/color] ... >[/color])

Peter-Paul Koch's [url="http://www.quirksmode.org/home.shtml"]QuirksMode[/url] has a clean and simple example of [url="http://www.quirksmode.org/js/formex.html"]cross-browser validation[/url].

Or, if you're just looking for a quick fix you could insert this into the HEAD of your html file:[code=php]<script type='text/javascript'>
String.prototype.isPercentage = function () {
var t = this.match(/^(d+)%?$/);
if (t != null && t.length == 2) {
t = parseInt(t[1]);
if (t >= 0 && t <= 100) return t/100;
}
return false;
}

function validate(f) {
var p = f.value.isPercentage();
if (!p) {
alert("You must enter a percentage between 0 and 100 in this field.");
f.value = "";
setTimeout(function () {f.focus()}, 10);
}
}
</script>[/code]
and use it in the input element with this syntax[code=html]onchange='validate(this)'[/code]
Copy linkTweet thisAlerts:
@alidar777authorJul 17.2006 — thanks
Copy linkTweet thisAlerts:
@alidar777authorJul 17.2006 — This is the PHP code, is there any Javascript code pls
Copy linkTweet thisAlerts:
@ExuroJul 17.2006 — This is the PHP code, is there any Javascript code pls[/QUOTE]
No, it's javascript. People on the forum just use the PHP Code tag so that it does all the spiffy automatic syntax highlighting.
×

Success!

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