i have a form within my website, this allows users to list items. now i am changing my policy where only items over $10 can be posted.
is there away to have a text field (price) not allow a value of less than $10 in. if the user puts a value of less than 10 in, it should display an error message such as. sorry your price must be over $10.
any help would be appreciated
If you have the following in your form:
Code:
<label><input type="text" name="blah" onkeyup="check(this)"><span></span></label>
So that the check() function is called whenever the value is changed, you can use a function like this to check that the value is less than 10:
Code:
function check(el){
var theSpan = el.parentNode.getElementsByTagName('span')[0];
if(parseInt(el.value) < 10 && theSpan.childNodes.length == 0){
theSpan.appendChild(document.createTextNode('min is 10'));
}else if(parseInt(el.value) >= 10){
theSpan.removeChild(theSpan.firstChild);
}
}
This adds some text in that span to tell the user what the problem is.
Was this answer helpful ?
Yes No
thanks i have got that working.
will this stop any value less than 10 submitting to the sql db.
the code given shows some text sayin min is 10 but the user can still click submit and this is sent to the db
Was this answer helpful ?
Yes No
Javascript alone cannot stop a form being submitted completely, so you need to do the validation server side too, but you make it better in JS too.
If you return a value from the check function, you can then call this onsubmit.
Code:
function check(el){
var theSpan = el.parentNode.getElementsByTagName('span')[0];
if(parseInt(el.value) < 10 && theSpan.childNodes.length == 0){
theSpan.appendChild(document.createTextNode('min is 10'));
}else if(parseInt(el.value) >= 10){
theSpan.removeChild(theSpan.firstChild);
return true;
}
return false;
}
Code:
<form action="..." method="..." onsubmit="return check(this.elementName)">
Was this answer helpful ?
Yes No