Loading

ASP.NET Validation Tips and Tricks (Part1)

Submitted by: 


Tips1#

Validate an IP address
We can easily validate an IP address by Adding Namespace

  1. using System.Text.RegularExpressions;
  2.  
  3. public class IPAddressTextBox: RegExTextBox
  4. {
  5. public IPAddressTextBox()
  6. {
  7. InitializeComponent();
  8. this.ValidationExpression = @"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$";
  9. this.ErrorMessage = "The IP address must be in the form of 111.111.111.111";
  10. }

Tips2#

Validate SSN(Social Security Number)

  1. using System.Text.RegularExpressions;
  2.  
  3. public class SsnTextBox: RegExTextBox
  4. {
  5. public SsnTextBox()
  6. {
  7. InitializeComponent();
  8. this.ValidationExpression = @"^\d{3}-\d{2}-\d{4}$";
  9. this.ErrorMessage = "The social security number must be in the form of 555-55-5555";
  10. }
  11. }

Tips3#

How to restrict length size in text box.

The following function is used to restrict length size in text box

  1. function MaxNumber(id,size)
  2. {
  3. if(id!=null)
  4. {
  5. var no =id.length;
  6. if(no>=size)
  7. {
  8. alert("Maximum length exceeds");
  9. event.returnValue=false;
  10. return false;
  11. }
  12. else
  13. {
  14. return true;
  15. }
  16. }//End of IF
  17.  
  18. }//End Of Funciton

Tips4#

How to validate an IP address from 1.0.0.0 to 255.255.2The following code sample will help in validating an IP address

  1. Public Function ValidIP(ByVal addr As String) As Boolean
  2. 'create our match pattern
  3. Dim pat As String = "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\." & _
  4. "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
  5. Dim check As New Text.RegularExpressions.Regex(pat)
  6. 'boolean variable to hold the status
  7. Dim valid As Boolean = False
  8. If addr = "" Then
  9. valid = False
  10. Else
  11. valid = check.IsMatch(addr, 0)
  12. End If
  13. 'return the results
  14. Return valid
  15. End Function
  16. 55.255

Tips5#

How to check and see if a string is all uppercase in c#

The following code sample helps to check and see if a string is all uppercase in c#. Pass the method a string, read the results (true/false). Need reference to

  1. System.Text.RegularExpressions Namespace
  2.  
  3. using System.Text.RegularExpressions;
  4.  
  5. public bool IsUppercase(string str) //string to check
  6. {
  7. bool upper; //to hold return value
  8. string pattern = "[a-z]"; //variable to hold the search pattern
  9. try
  10. {
  11. Regex AllCaps = new Regex(pattern);
  12. if (AllCaps.IsMatch(str))
  13. {
  14. upper = false;
  15. }
  16. upper = true;
  17. }
  18. catch
  19. {
  20. upper = false;
  21. }
  22. return upper;
  23. }

About the author:

Planet Source Code is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development




Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.