How to write javaScript function for this?
I have a textbox and i write javascript function that if it is blank then it will generate alertbox . but i press space in that textbox then it doesnot generate alertbox.
so i want that if i give space then it also generate alertbox, for this what to do?
I used this function:
function Trim(objValue) {
var lRegExp = /^\s+/;
var rRegExp = /\s+$/;
objValue = objValue.replace(lRegExp, ''); //Perform LTRim
objValue = obj开发者_如何学PythonValue.replace(rRegExp, ''); //perform RTrim
return objValue;
}
function ValidateTextBoxIncome() {
var txtEnterItems = document.getElementById("txtEnterItems");
if (Trim(txtEnterItems) == '') {
alert("Cannot be blank");
return false;
}
}
where is the error? please suggest me.
You're passing the DOM object to your function, but intended to pass the value of that textbox. Replace this line:
if (Trim(txtEnterItems) == '') {
by:
if (Trim(txtEnterItems.value) == '') {
Replace
var txtEnterItems = document.getElementById("txtEnterItems");
with
var txtEnterItems = document.getElementById("txtEnterItems").value;
Try the following:
function ValidateTextBoxIncome() {
var txtEnterItems = document.getElementById("txtEnterItems");
if (Trim(txtEnterItems.value).length == 0) {
alert("Cannot be blank");
return false;
}
}
精彩评论