---
title: "HOW TO ADD FOCUS TO A FIELD WHEN THE USER LEAVES THE FIELD BLANK IN MY FORM?"  
description: "HOW TO ADD FOCUS TO A FIELD WHEN THE USER LEAVES THE FIELD BLANK IN MY FORM?"  
author: "Anonymous User"  
published: 2017-12-26  
updated: 2017-12-26  
canonical: https://www.mindstick.com/forum/34414/how-to-add-focus-to-a-field-when-the-user-leaves-the-field-blank-in-my-form  
category: "c#"  
tags: ["asp.net mvc", "mvc", "mvc4", "razor"]  
reading_time: 2 minutes  

---

# HOW TO ADD FOCUS TO A FIELD WHEN THE USER LEAVES THE FIELD BLANK IN MY FORM?

Hi,

I want to integrate autofocus into my [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) when the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) leaves the [field](https://www.mindstick.com/forum/160867/does-mindstick-provide-training-for-field-marketing) blank

Below is my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii),

```
<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.

## Replies

### Reply by Hemant Patel

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...


---

Original Source: https://www.mindstick.com/forum/34414/how-to-add-focus-to-a-field-when-the-user-leaves-the-field-blank-in-my-form

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
