tags:

views:

65

answers:

3

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.

+3  A: 

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) == '') {
Lekensteyn
Thanks sir,I have done mistake.Now its working fine.
Shalni
A: 

Replace

var txtEnterItems = document.getElementById("txtEnterItems");

with

var txtEnterItems = document.getElementById("txtEnterItems").value;
Yogesh
A: 

Try the following:

function ValidateTextBoxIncome() {
    var txtEnterItems = document.getElementById("txtEnterItems");
    if (Trim(txtEnterItems.value).length == 0) {
        alert("Cannot be blank");
        return false;
    }
}
Suresh Kumar
I never saw a mention of a framework like jQuery, so I expect pure Javascript. `String.length` is not a function.
Lekensteyn
@Lekensteyn: You are right. length is not a function but a property on the string object. I have changed the code accordingly.
Suresh Kumar