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.
@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]
@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.