Automatic filling of Textbox

Asked

Viewed 179 times

2

I have the table AspNetUser in my database that I use to identify and authorize logins.

To login to my page, the user always needs to type the [email protected].

It is possible to program the TextBox to automatically fill the "@email with." ?

So the user would only need to fill in the Textbox: "user".

I want to follow the same idea of the Google site, where we do not need to fill the @gmail.with, just enter the first part of the email.

I’ve tried that:

<script>
function autoCompletEmail() {
    var x = document.getElementById("email");
    x.value = x.value + "wstur.com";
}

<div class="form-group">
                @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                <div class="col-md-10" id="email" onblur="autoCompletEmail">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                </div>
            </div>
  • Use the onleave textbox event to complete with @provider.com

  • Thank you. Can you give an example of how you use it? I’m new to this area of ASP NET MVC.

1 answer

2

You can always use the event onblur and then complement the @email with. client-side:

<input type="text" id="email" onblur="addEmailSufix()">

On the client’s side:

<script>
    function addEmailSufix()
    {
        var x = document.getElementById("email");
        x.value = x.value + encodeURIComponent("@gmail.com");
    }
</script>

Using the example you posed in your question:

<script>
    function autoCompleteEmail() {
        var x = document.getElementById("email");
        x.value = x.value + encodeURIComponent("@wstur.com");
    }
</script>

<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @id = "email", @class = "form-control", @onblur = "autoCompleteEmail();" })
        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
    </div>
</div>
  • Thanks. I tried, but it didn’t work. What’s wrong with my code above?

  • Added new solution to the answer!

  • This error appears: "Compiler Error Message: CS0103: The name 'wstur' does not exist in the Current context". Gives error when I put "@". It only goes "wstur.com".

  • Edited response :)

  • Thanks. But, it hasn’t worked yet. Same error persists.

  • Try to put a "" bar before the @.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.