TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
2.1k
481.9k
How to validate error message on web form?
Apr 7 2020 5:53 AM
Hi Team
I want to validate when leaving textbox, error message of 5 textbox. Each of these except one textbox field must have error label red. Meaning i have street name, street address line2, city, post code, country, state/province. As soon you leave textbox(State/Province) It must validate error message "Field is required". I have tried using model Required data notation i could not success, please view my logic below.
// Model class
[Required(ErrorMessage =
"This field is required"
)]
public
string
State {
get
;
set
; }
```
View.cshtml
```html
<div
class
=
"row"
>
<label
for
=
"Address"
>Address</label>
<div
class
=
"col-md-6 "
>
<div
class
=
"input-group pull-right"
>
@Html.TextBoxFor(m => m.HomeMainModel.Address,
new
{ @
class
=
"form-control"
, type =
"text"
, id =
"inputFormVal"
,autofocus =
"autofocus"
, placeholder =
"Street Address"
, required =
"required"
})
<div
class
=
"input-group-append"
>
<div
class
=
"input-group-text"
>
</div>
</div>
</div>
</div>
<div
class
=
"col-md-6"
>
<label id=
"labelMessageBx"
class
=
"text-danger"
style=
"display:none"
></label>
</div>
</div>
<hr />
<div
class
=
"row"
>
<div
class
=
"col-md-6 "
>
<div
class
=
"input-group pull-right"
>
@Html.TextBoxFor(m => m.HomeMainModel.Address,
new
{ @
class
=
"form-control"
, type =
"text"
, id=
"inputFormVal"
, autofocus =
"autofocus"
, placeholder =
"Street Address Line 2"
, required =
"required"
})
<div
class
=
"input-group-append"
>
<div
class
=
"input-group-text"
>
</div>
</div>
</div>
</div>
<div
class
=
"col-md-6"
>
<label id=
"labelMessageBx"
class
=
"text-danger"
style=
"display:none"
></label>
</div>
</div>
<hr />
<div
class
=
"form-group"
>
<div
class
=
"input-group mb-2"
>
<div
class
=
"input-group-prepend"
>
</div>
<div
class
=
"input-group col-md-7 col-md-offset-7 col-sm-7 col-xs-7"
>
<div
class
=
"input-group pull-right"
>
@Html.TextBoxFor(m => m.HomeMainModel.City,
new
{ @
class
=
"form-control"
, type =
"text"
, id =
"inputFormVal"
, autofocus =
"autofocus"
, placeholder =
"City"
})
@Html.TextBoxFor(m => m.HomeMainModel.Code,
new
{ @
class
=
"form-control"
, type =
"text"
, id =
"inputFormVal"
, autofocus =
"autofocus"
, placeholder =
"Province"
})
</div>
</div>
</div>
<div
class
=
"col-md-6"
>
<label id=
"labelMessageBx"
class
=
"text-danger"
style=
"display:none"
></label>
</div>
<hr />
<!--Zip code
for
postal code-->
<div
class
=
"input-group mb-2"
>
<div
class
=
"input-group-append"
>
</div>
<div
class
=
"input-group col-md-7 col-md-offset-3 col-sm-2 col-xs-2"
>
<div
class
=
"input-group pull-right"
>
@Html.TextBoxFor(m => m.HomeMainModel.Code,
new
{
@
class
=
"form-control"
,
type =
"text"
,
id =
"inputFormVal"
,
autofocus =
"autofocus"
,
placeholder =
"Postal/Zip Code"
})
@Html.DropDownListFor(m => m.HomeMainModel.SelectedCountryId,
this
.ViewBag.CountryList
as
SelectList,
new
{ @
class
=
"form-control"
})
<div
class
=
"input-group-append"
>
<div
class
=
"input-group-text"
>
</div>
</div>
</div>
</div>
<div
class
=
"col-md-6"
>
<label id=
"labelMessageBx"
class
=
"text-danger"
style=
"display:none"
></label>
</div>
</div>
</div>
```
```javascript
// function when leaving texbox for city, street address, street_address_line2, state_province.
$(function() {
$(
'#inputFormVal'
).blur(function() {
var city = document.getElementById(
"inputFormVal"
).value;
var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if
(!expr.test(city)) {
document.getElementById(
"labelMessageBx"
).style.display =
"inline"
;
}
else
{
document.getElementById(
"labelMessageBx"
).style.display =
"none"
;
}
});
});
```
Reply
Answers (
2
)
Server control - drop down list :using the right property?
how to prevent loading querystring parameter again