开发者

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜