. Net MVC foreground validation and background validation

Source: Internet
Author: User

Front Desk verification:

First you have to have a parameter class, the parameter class code is as follows

Summary of Validation tokens

[DisplayName ("Email:")]
[Required (errormessage = "Please enter your email")]
[RegularExpression (@ "^ (\w-*\.*) [email protected] (\w-?) + (\.\w{2,}) +$ ", errormessage =" Please enter the correct mailbox ")]
public string Email {get; set;}

And then go back to the view. Validating parameter classes with Validactionmessagefor

<p>
<label id= "Label_mail" for= "mail" > Email </label>
@Html. textboxfor (model = Model.email, new {@class = "user_input", TabIndex = "4"})
@Html. validationmessagefor (model = model.email)

</p>

Background verification: Personally think it is more appropriate to write a validation class,

Validation class:

public bool Isemail (string isemail) {
Regex regchzn = new Regex (@ "^ (\w-*\.*) [email protected] (\w-?) + (\.\w{2,}) +$ ");
Match m = Regchzn.match (Isemail);
return m.success;
}

Call in the controller to determine if validation succeeds:

private bool Validate (Field field)
{

BOOL Isemail=validate. Isemail (Field.email);

if (isemail==true)

{

return true;

}

else{

return false;

}

}

if (modelstate.isvalid)
{

Used to determine whether the front-end verification

if () {//is used here to write back-end validation

}
}

Pure hand, reproduced please show the original link

. Net MVC foreground validation and background validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.