admin管理员组文章数量:1410717
I own a canvas website and want my customers to be able to enter a custom length of the canvas within a set range.
Say the range for the product is:
- Minimum: 10 cm
- Maximum: 200 cm
Then in the text box they can enter any number between that range, but if they enter "215" then it should automatically go down to "200". Likewise if they enter "7" then it should automatically go up to "10"
I own a canvas website and want my customers to be able to enter a custom length of the canvas within a set range.
Say the range for the product is:
- Minimum: 10 cm
- Maximum: 200 cm
Then in the text box they can enter any number between that range, but if they enter "215" then it should automatically go down to "200". Likewise if they enter "7" then it should automatically go up to "10"
Share asked Jun 20, 2011 at 15:45 MichaelMichael 2352 gold badges6 silver badges17 bronze badges 1-
Input text fields have a
maxlength
attribute, but there's no minlength. You can put some javascript in there to detect insufficient data, as long as you do the same check server-side afterwards. – Marc B Commented Jun 20, 2011 at 15:48
3 Answers
Reset to default 6document.getElementById('textBoxID').onchange = function(){
if(this.value > 200){
this.value = 200;
}
if(this.value < 10){
this.value = 10;
}
}
Here is a fiddle example: http://jsfiddle/maniator/qwFN6/
<input type="text"
onchange="this.value = (this.value > 215) ? 215 : ((this.value < 10) ? 10 : this.value);">
Find below jQuery based code
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<style>
#customForm input.error{
background: #f8dbdb;
border-color: #e77776;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
//global vars
var form = $("#customForm");
var canvas = $("#canvasID");
var canvasInfo= $("#canvasInfo");
//On blur
canvas.blur(validateCanvas);
//On Submitting
form.submit(function(){
if(validateCanvas())
return true
else
return false;
});
function validateCanvas(){
//if it's NOT valid
if(canvas.val()< 10 || canvas.val()>200){
if(canvas.val()< 10) {
canvas.val('10');
}
if(canvas.val()>200) {
canvas.val('200');
}
canvas.addClass("error");
canvasInfo.text("We want canvas Minimum: 10 cm and Maximum: 200 cm");
return false;
}
//if it's valid
else{
canvas.removeClass("error");
canvasInfo.text("");
return true;
}
}
});
</script>
本文标签: phpSetting a maximum and minimum value for text boxStack Overflow
版权声明:本文标题:php - Setting a maximum and minimum value for text box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744807751a2626252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论