HOW TO ADD FOCUS TO A FIELD WHEN THE USER LEAVES THE FIELD BLANK IN MY FORM?

Total Post:3

Points:21
C#
 1119  View(s)
Ratings:
Rate this:

Hi,

I want to integrate autofocus into my application when the user leaves the field blank

Below is my code,

<div class="form-group">

                <label for="Username">Email or Username</label>
                @Html.TextBoxFor(x => x.EmailID, new { @maxlength = 100, @class = "qa-form-tall-text", placeholder = "User Name" })
                @Html.ValidationMessageFor(x => x.EmailID)
            </div>
            <div class="form-group">
                <label for="Username">Password</label>
                @Html.PasswordFor(x => x.Password, new { @maxlength = 200, @class = "qa-form-tall-text", placeholder = "Password" })
                @Html.ValidationMessageFor(x => x.Password)<br />
            </div>. 

Please provide some appropriate solution to accomplish my task.

  1. Post:44

    Points:310
    Re: HOW TO ADD FOCUS TO A FIELD WHEN THE USER LEAVES THE FIELD BLANK IN MY FORM?

    Hello,

    Sophie you can use autofocus attribute in your code to achieve your task.

    Please follow the below-modified line of code to achieve your task,

    <div class="form-group">
    

                    <label for="Username">Email or Username</label>

                    @Html.TextBoxFor(x => x.EmailID, new { @maxlength = 100, @class = "qa-form-tall-text", placeholder = "User Name", @autofocus = true })

                    @Html.ValidationMessageFor(x => x.EmailID)

                </div>

                <div class="form-group">

                    <label for="Username">Password</label>

                    @Html.PasswordFor(x => x.Password, new { @maxlength = 200, @class = "qa-form-tall-text", placeholder = "Password" , @autofocus = true})

                    @Html.ValidationMessageFor(x => x.Password)<br />

                </div>

    I hope it's informative for you...

      Modified On Dec-26-2017 01:28:23 AM

Answer