During the login function or some other secure pages development, we always need string validation to ensure the data correction before it is inserted database. In this article I note them during each developing work.
1. 檢查字串是否包含特定字元 | Contains specify symbol, letter etc.
System.Text.RegularExpressions.Regex.IsMatch(argValue, "[A-Z]")
System.Text.RegularExpressions.Regex.IsMatch(argValue, "[0-9]")
System.Text.RegularExpressions.Regex.IsMatch(argValue, "[a-z]")
System.Text.RegularExpressions.Regex.IsMatch(argValue, "[/u0000-/u00FF]")
2. 檢查字串是否為E-mail格式 | Email format
Regex.IsMatch(emailAddress,
@"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
沒有留言:
張貼留言