ASP.NET WebAPI2第005天_API驗證
不管是面對User的WebSite或者網站程式端對端的API
都需要對傳入資料進行驗證
新增一個Customer的DTO Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace MyWebAPI2_Test0.Models { public class Customer { public int Id { get; set; } [Required, StringLength(15)] public string Name { get; set; } [RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,})+)$", ErrorMessage = "Email in not valid")] public string Email { get; set; } [RegularExpression("^[0-9]+$", ErrorMessage = "Invalid Phone Number")] public string Phone { get; set; } } } |
一些正則表達式驗證補充(這邊用簡單還沒這麼嚴謹查檢)
Email 格式驗證正則表達式 ^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$ ^([\w\.\-]+)@([\w\-]+)((\.(\w){2,})+)$ ^ 比對輸入列的啟始位置 $ 比對輸入列的結束位置 (pattern) 匹配pattern並獲取這一匹配 [字元集合] 比對中括弧內的任一個字元 []+ 一或多個 \w 數字、字母、底線 [a-zA-Z0-9_] \.\- 「-」或是「.」 比方 @tintint.co @email.coursera.org @acer-mail.com @hahow.in @reply.agoda-emails.com @taaze-fun.com @nedm.ubot.com.tw @mail.member-mail.ithome.com.tw {2,3} 最少匹配到2次且最多匹配3次(或以上),比方 .com, .com.tw, .dks.com.tw, .org, .cc, .jp, .co, {2,} 最少匹配到2次以上,比方.info .foodpanda.com.tw, .netflix.com, .post.gov.tw, .buy123.com.tw, 電話號碼 正則表達式 ^[0-9]*$ 至少可0個數字,最多不限制數字字元數 ^[0-9]+$ 至少一個數字,最多不限制數字字元數
跟Customers API控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using MyWebAPI2_Test0.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace MyWebAPI2_Test0.Controllers { public class CustomersController : ApiController { static List<Customer> customers = new List<Customer>() { new Customer(){Id=1,Name = "Tom Cruise",Email = "tomcruise@gmail.com",Phone = "3322"}, new Customer(){Id=1,Name = "Robert Downy Jr",Email = "robert@gmail.com",Phone = "326"}, new Customer(){Id=1,Name = "Chris patt",Email = "cpatt@hotmail.com",Phone = "659"}, }; // GET: api/Customers public IEnumerable<Customer> Get() { return customers; } // GET: api/Customers/5 public string Get(int id) { return "value"; } // POST: api/Customers public IHttpActionResult Post([FromBody] Customer customer) { if (ModelState.IsValid) { customers.Add(customer); return Ok(); } return BadRequest(ModelState); } // PUT: api/Customers/5 public void Put(int id, [FromBody] string value) { } // DELETE: api/Customers/5 public void Delete(int id) { } } } |
Ref:
https://stackoverflow.com/questions/5342375/regex-email-validation
https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
https://ithelp.ithome.com.tw/articles/10094951
https://stackoverflow.com/questions/16167983/best-regular-expression-for-email-validation-in-c-sharp/16168118
https://help.xmatters.com/ondemand/trial/valid_email_format.htm
https://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number/16702965
留言
張貼留言