1

I have a <%: Html.TextBoxFor(model=>model.UserName)%>. Here I'm checking wheather user name is available or not. If it's not available then I need to clear the TextBox. I used the query $("#UserName").val("");. But after the event completion again the text box getting the value. Can anyone help me?

UPDATE: Additional Javascript code from comments:

function CheckAvailability() {
    $.post("/WebTeamReleaseDB/CheckAvailability", {
        Username: $("#UserName").val()
    }, function(data) {
        var myObject = eval('(' + data + ')');
        var newid = myObject;
        if (newid == 1) {
            $("#usernamelookupresult").html("<font color='green'>Available :-D</font>")
        } else {
            $("#UserName").val("");
            $("#usernamelookupresult").html("<font color='red'>Taken :-(</font>")
        }
    });
}
9
  • Is the element's id UserName? Commented Jun 8, 2011 at 14:00
  • Could you post a little more code? Commented Jun 8, 2011 at 14:00
  • I'm using Html.TextBoxFor(). Here how can I use the element id? Commented Jun 8, 2011 at 14:02
  • function CheckAvailability() { $.post("/WebTeamReleaseDB/CheckAvailability", {Username: $("#UserName").val()},function (data) { var myObject = eval('(' + data + ')');var newid = myObject;if (newid == 1) { $("#usernamelookupresult").html("<font color='green'>Available :-D</font>")} else {$("#UserName").val(""); $("#usernamelookupresult").html("<font color='red'>Taken :-(</font>")}});} Commented Jun 8, 2011 at 14:04
  • which event are you talking about? Commented Jun 8, 2011 at 14:06

2 Answers 2

1

When you use <%: Html.TextBoxFor(model=>model.UserName)%>, it may be rendering to the page as <input name="UserName">, so $("#UserName").val(""); is not finding it, as there is no id="UserName".

One possible solution: use <%: Html.TextBoxFor(model=>model.UserName, new { id = "UserName" })%>, which should render to the page as <input name="UserName" id="UserName">. After that, $("#UserName").val(""); should work, since the textbox now has that id attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried ur code also. But its its clearing the textbox until the java script event completes its execution. After execution completes, textbox getting the value.
@Davis: I got it Davis. I have written like this: <%: Html.TextBoxFor(model=>model.UserName, new { @onchange=CheckAvailability()}
0

I got the answer for my problem.

HTML code:

<%: Html.TextBoxFor(model=>model.UserName,new { @onchange="CheckAvailability()" }) %>

Javascript Code:

function CheckAvailability() {
        var value;
        if (document.getElementById("UserName").value == "") {
            $("#usernamelookupresult").html("");
            alert("Please Enter User Name");
        }
        else {
            $.post("/WebTeamReleaseDB/CheckAvailability",
            {
                Username: $("#UserName").val()
            },
                function (data) {
                    var myObject = eval('(' + data + ')');
                    var newid = myObject;
                    value = myObject;
                    if (newid == 1) {
                        $("#usernamelookupresult").html("<font color='green'>Available :-D</font>")
                    }
                    else if (newid == 2) {
                        $("#usernamelookupresult").html("<font color='red'>UserName Invalid! :-(</font>")
                    }
                    else {
                        $("#usernamelookupresult").html("<font color='red'>Taken :-(</font>")
                        $("#UserName").val("");
                    }
                });
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.