In this blog, we will create a registration form in ASP.NET, using Bootstrap and basic Manual Validation, in C# and will be showing the bootstrap alerts to the users.

Design

Step 1 - Go to Visual Studio and create new ASP.NET web application.
Step 2 - Create a new Webpage like index.aspx. Design the page as shown below.

output

Here is the HTML code of the Index page design, shown above.

  1. <html>
  2. <head runat="server">
  3. <title>Manual validation using c#</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  8. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  9. <style type="text/css">
  10. .messagealert {
  11. width: 100%;
  12. position: absolute;
  13. top: 40%;
  14. z-index: 999;
  15. padding: 0;
  16. font-size: 15px;
  17. text-align: center;
  18. }
  19. </style>
  20. <script type="text/javascript">
  21. function ShowMessage(message, messagetype) {
  22. var cssclass;
  23. switch (messagetype) {
  24. case 'Success':
  25. cssclass = 'alert-success'
  26. break;
  27. case 'Error':
  28. cssclass = 'alert-danger'
  29. break;
  30. case 'Warning':
  31. cssclass = 'alert-warning'
  32. break;
  33. default:
  34. cssclass = 'alert-info'
  35. }
  36. $('#alert_container').append('<div id="alert_div" style="margin: 0 0.5%; -webkit-box-shadow: 3px 4px 6px #999;" class="alert fade in ' + cssclass + '"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><strong>' + messagetype + '!</strong> <span>' + message + '</span></div>');
  37. }
  38. </script>
  39. </head>
  40. <body>
  41. <form id="form1" runat="server">
  42. <div class="messagealert" id="alert_container">
  43. </div>
  44. <div class="container">
  45. <div class="row">
  46. <div class="col-md-4 col-md-offset-4" style="padding: 10px; margin-top: 2em;">
  47. <div class="panel panel-success">
  48. <div class="panel-heading">Registeration Form</div>
  49. <div class="panel-body">
  50. <label>User Name</label>
  51. <asp:TextBox ID="uname" runat="server" CssClass="form-control" placeholder="User Name"></asp:TextBox>
  52. <label>Last Name</label>
  53. <asp:TextBox ID="lname" runat="server" CssClass="form-control" placeholder="Last Name"></asp:TextBox>
  54. <label>Mobile</label>
  55. <asp:TextBox ID="mobile" runat="server" CssClass="form-control" placeholder="Mobile"></asp:TextBox>
  56. <label>Email</label>
  57. <asp:TextBox ID="email" runat="server" CssClass="form-control" placeholder="Email"></asp:TextBox>
  58. <br />
  59. </div>
  60. <div class="panel-footer"> <asp:Button ID="Button1" runat="server" CssClass=" btn btn-block btn-success" Text="Submit" OnClick="Button1_Click" /></div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </form>
  66. </body>
  67. </html>
Code Behind
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. namespace ValidationBottstrapAlerts
  9. {
  10. public partial class index : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. public enum MessageType { Success, Error, Info, Warning };
  16. protected void ShowMessage(string Message, MessageType type)
  17. {
  18. ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + type + "');", true);
  19. }
  20. protected void Button1_Click(object sender, EventArgs e)
  21. {
  22. //check empty fields
  23. if (uname.Text == "" && lname.Text == "" && mobile.Text == "" && email.Text == "")
  24. {
  25. //Error alert
  26. ShowMessage("Please Enter the fields!!" + "<br/>User name, Last Name, Mobile Number, Email", MessageType.Error);
  27. }
  28. //check special characters
  29. else if (uname.Text.Contains('+') || uname.Text.Contains('-') || uname.Text.Contains('*') || uname.Text.Contains('/'))
  30. {
  31. //Warning Alert
  32. ShowMessage("Special characters not allowed in User Name fields!!", MessageType.Warning);
  33. }
  34. //Check Mobile number
  35. else if (mobile.Text.Length < 10)
  36. {
  37. ShowMessage("Invalid Your Mobile number!!"+mobile.Text, MessageType.Error);
  38. }
  39. //Check Email Id is valid
  40. else if (!IsValidEmailId(email.Text))
  41. {
  42. //Error alert
  43. ShowMessage("Invalid Your Email-ID!!"+email.Text, MessageType.Error);
  44. }
  45. else
  46. {
  47. //Success Alert
  48. ShowMessage("Successfully completed|"+uname.Text+"|"+lname.Text+"|"+mobile.Text+"|"+email.Text, MessageType.Success);
  49. }
  50. }
  51. private bool IsValidEmailId(string InputEmail)
  52. {
  53. //Regex To validate Email Address
  54. Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
  55. Match match = regex.Match(InputEmail);
  56. if (match.Success)
  57. return true;
  58. else
  59. return false;
  60. }
  61. }
  62. }
Output

output

output
Thus, you can see in the above screenshot that users get alert messages whenever an entry is not valid.