Reputation: 1027
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 = objValue.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.
Upvotes: 0
Views: 207
Reputation: 11787
Try the following:
function ValidateTextBoxIncome() {
var txtEnterItems = document.getElementById("txtEnterItems");
if (Trim(txtEnterItems.value).length == 0) {
alert("Cannot be blank");
return false;
}
}
Upvotes: 0
Reputation: 4075
Replace
var txtEnterItems = document.getElementById("txtEnterItems");
with
var txtEnterItems = document.getElementById("txtEnterItems").value;
Upvotes: 0
Reputation: 66535
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) == '') {
Upvotes: 3