HTML and CSS Reference
In-Depth Information
is submitted. If a customer ID is found,
setCustomValidity()
method is called with an empty string
indicating that there is no validation error. The
ValidateCustomerID()
action is shown in Listing 5-10.
Listing 5-10.
ValidateCustomerID()
Action Method
public JsonResult ValidateCustomerID(string id)
{
NorthwindEntities db = new NorthwindEntities();
var data = from item in db.Customers
where item.CustomerID == id
select item;
if (data.Count() <= 0)
{
return Json(false);
}
else
{
return Json(true);
}
}
The
ValidateCustomerID()
action uses the Entity Framework data model for the
Northwind
database to
determine whether a
CustomerID
value exists in the database. Accordingly, a
true
or
false
flag is returned
as
JsonResult
. Figure 5-13 shows a sample run of the view with an invalid customer ID.
Figure 5-13.
setCustomValidity()
method in action
Notice an interesting thing about Listing 5-9: the
$.ajax()
call specifies an
async
option value of
false
. By default,
$.ajax()
makes an asynchronous request to the server. However, with this default
behavior, the Submit button submits the form without waiting for
ValidateCustomerID()
to return. With
the
async
option set to
false
,
$.ajax()
makes a synchronous request to the server rather than
asynchronous, and the
OnSubmit
event handler waits for the
ValidateCustomerID()
method to complete.
HTML5 Input Types and ASP.NET Validation Techniques
Although the new HTML5 input types are great additions to the HTML markup, at times you need to
mix HTML5 native validation techniques and ASP.NET validation techniques. ASP.NET Web Forms and