I have had great success performing validation using AJAX using data annotation attributes. To verify the correctness of your data, you will want to use the controller ModelState property, which has its own IsValid property. I highly recommend looking into the data annotation attribute attribute guide on the ASP.NET MVC official site.
First, you will want to change the action of your controller to accept the model object as a parameter, rather than a separate account name and number. This will make validation, which I will demonstrate below, much easier. From your example, I think your model object will or will be called by Customer. You may have the following code to determine the model object and the actions of your controller ...
// model object public class Customer { public Int32 Id {get; set;} public String Name {get; set;} public String AccountNumber {get; set;} } // controller public class CustomerController : Controller { public ActionResult CreateCustomer( [Bind(Exclude = "Id")] Customer customer ) { // controller action code } }
Make sure the form fields are named according to the property names of the Customer object, so ASP.NET MVC can automatically link them. The "Bind" attribute in this case tells ASP.NET MVC to ignore the "Id" property of the Customer class when binding form fields to model properties. Since this is a new client, we do not yet have an identifier, so we can safely leave Id as independent of the default value and leave a data layer to determine how it is best generated.
Once the controller has built a model object for the action method, its validity can be easily checked using the ModelState.IsValid property. As expected, it will return true if the model properties are valid or false if 1 or more properties are invalid.
From the original question, it turns out that the CustomerService.InsertCustomer method throws exceptions when the validation fails. This is completely unnecessary. InsertCustomer should only perform any data operations necessary to insert a new record. If you do not want to abstract from specific implementation exceptions, such as SqlException, InsertCustomer should not really catch or throw any exceptions, but most likely just allow any exceptions to bubble up to the controller (or whoever may be the caller).
The end result of all this may be a controller action that looks like this:
public ActionResult CreateCustomer( [Bind(Exclude = "Id")] Customer customer ) { // model is invalid if (!ModelState.IsValid) { return Json(new ActionInfo() { Success = false, Message = "Validation failed" // you will probably want a more robust message :-) }); } // service method accepts a Customer object rather than arbitrary strings CustomerService.InsertCustomer(customer); return Json(new ActionInfo() { Success = true, Message = "Customer created successfully." }); }
If you want to report unexpected errors, such as database related exceptions, then you can add a try / catch block around the InsertCustomer call and return the necessary result to display the error message back to the client.