Validation Controls in ASP
Validation Controls in ASP
NET
An important aspect of creating ASP.NET Web pages for user input is to be able
to check that the information users enter is valid. ASP.NET provides a set of
validation controls that provide an easy-to-use but powerful way to check for
errors and, if necessary, display messages to the user.
There are six types of validation controls in ASP.NET
1. RequiredFieldValidation Control
2. CompareValidator Control
3. RangeValidator Control
4. RegularExpressionValidator Control
5. CustomValidator Control
6. ValidationSummary
The below table describes the controls and their work,
Validation Control Description
RequiredFieldValidation Makes an input control a required field
Compares the value of one input control to the
CompareValidator
value of another input control or to a fixed value
Checks that the user enters a value that falls
RangeValidator
between two values
Ensures that the value of an input control matches
RegularExpressionValidator
a specified pattern
Allows you to write a method to handle the
CustomValidator
validation of the value entered
Displays a report of all validation errors occurred
ValidationSummary
in a Web page
All validation controls are rendered in form as <span> (label are referred as
<span> on client by server)
Important points for validation controls
ControlToValidate property is mandatory to all validate controls.
One validation control will validate only one input control but
multiple validate control can be assigned to a input control.
Validation Properties
Usually, Validation is invoked in response to user actions like clicking submit
button or entering data. Suppose, you wish to perform validation on page when
user clicks submit button.
Server validation will only performed when CauseValidation is set to true.
When the value of the CausesValidation property is set to true, you can also use
the ValidationGroup property to specify the name of the validation group for
which the Button control causes validation.
Page has a Validate() method. If it is true this methods is executed. Validate()
executes each validation control.
To make this happen, simply set the CauseValidation property to true for submit
button as shown below:
<asp:Button ID="Button2" runat="server" Text="Submit"
CausesValidation=true />
Lets understand validation controls one by one with practical demonstration:
A PHP Error occurs when something is wrong in the PHP code. The error can be as
simple as a missing semicolon, or as complex as calling an incorrect variable.
To efficiently resolve a PHP issue in a script, you must understand what kind of
problem is occurring.
1. Warning Error
2. Notice Error
3. Parse Error
4. Fatal Error
Warning Error
A warning error in PHP does not stop the script from running. It only warns you that
there is a problem, one that is likely to cause bigger issues in the future.
For instance:
<?php
echo "Warning error"';
include ("external_file.php");
?>
Notice Error
Notice errors are minor errors. They are similar to warning errors, as they also don’t
stop code execution. Often, the system is uncertain whether it’s an actual error or
regular code. Notice errors usually occur if the script needs access to an undefined
variable.
Example:
<?php
$a="Defined error";
echo "Notice error";
echo $b;
?>
For example, the following script would stop execution and signal a parse error:
<?php
echo "Red";
echo "Blue";
echo "Green"
?>
1. Startup fatal error (when the system can’t run the code at installation)
2. Compile time fatal error (when a programmer tries to use nonexistent data)
3. Runtime fatal error (happens while the program is running, causing the code
to stop working completely)
<?php
function sub()
{
$sub=6-1;
echo "The sub= ".$sub;
}
div();
?>
Create a simple HTML form and accept the user name and display the name
through PHP echo statement.
HTML form: A webform or HTML form on a web page allows a user to enter
data that is sent to a server for processing. Forms can resemble paper or
database forms because web users fill out the forms using checkboxes, radio
buttons, or text fields. For example, forms can be used to enter railway or
credit card data to purchase a product, or can be used to retrieve search
results from a search engine.
Sample Solution: -
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
</head>
<body>
<form method='POST'>
<h2>Please input your name:</h2>
<input type="text" name="name">
<input type="submit" value="Submit Name">
</form>
<?php
//Retrieve name from query string and store to a local variable
$name = $_POST['name'];
echo "<h3> Hello $name </h3>";
?>
</body>
</html>
Sample Output:
Flowchart:
ASP.NET life cycle specifies, how:
Initialization
Instantiation of the controls on the page
Restoration and maintenance of the state
Execution of the event handler codes
Page rendering
Understanding the page cycle helps in writing codes for making some specific thing happen
at any stage of the page life cycle. It also helps in writing custom controls and initializing
them at right time, populate their properties with view-state data and run control behavior
code.
Following are the different stages of an ASP.NET page:
Page request - When ASP.NET gets a page request, it decides whether to
parse and compile the page, or there would be a cached version of the page;
accordingly the response is sent.
Starting of page life cycle - At this stage, the Request and Response objects
are set. If the request is an old request or post back, the IsPostBack property of
the page is set to true. The UICulture property of the page is also set.
Page initialization - At this stage, the controls on the page are assigned unique
ID by setting the UniqueID property and the themes are applied. For a new
request, postback data is loaded and the control properties are restored to the
view-state values.
Page load - At this stage, control properties are set using the view state and
control state values.
Validation - Validate method of the validation control is called and on its
successful execution, the IsValid property of the page is set to true.
Postback event handling - If the request is a postback (old request), the
related event handler is invoked.
Page rendering - At this stage, view state for the page and all controls are
saved. The page calls the Render method for each control and the output of
rendering is written to the OutputStream class of the Response property of
page.
Unload - The rendered page is sent to the client and page properties, such as
Response and Request, are unloaded and all cleanup done.
1. <body>
2. <form id="form1" runat="server">
3. <div>
4.
5. <asp:Label ID="Label1" runat="server" Text="Userna
me"></asp:Label>
6. <asp:TextBox ID="TextBox1" runat="server" Width="1
46px"></asp:TextBox>
7. <asp:RequiredFieldValidator ID="RequiredFieldValid
ator1" runat="server"
8. ControlToValidate="TextBox1" ErrorMessage="Ent
er username." ForeColor="Red"></
asp:RequiredFieldValidator>
9. <br />
10. Password<asp:TextBox ID="TextBox2" runat="server"
TextMode="Password"
11. Width="147px"></asp:TextBox>
12. <asp:RequiredFieldValidator ID="RequiredFieldVali
dator2" runat="server"
13. ControlToValidate="TextBox2" ErrorMessage="En
ter password." ForeColor="Red"></
asp:RequiredFieldValidator>
14. <br />
15. <asp:Button ID="Button1" runat="server" onclick="
Button1_Click" Text="Log In" />
16. <br />
17. <asp:Label ID="Label2" runat="server"></
asp:Label>
18. </div>
19. </form>
20. </body>
https://www.geeksforgeeks.org/directives-in-jsp/
https://www.geeksforgeeks.org/how-to-calculate-multiplication-and-division-of-two-numbers-using-
javascript/
OR
const array = [1, 2, 3, 4, 5, 6];
let s = 0;
let p = 1;
let i;
for (i = 0; i < array.length; i += 1)
{
s += array[i];
p *= array[i];
}
console.log(`Sum : ${s} Product : ${p}`);
https://www.javatpoint.com/how-to-check-a-radio-button-using-javascript
https://www.tutorialspoint.com/javascript/javascript_events.htm#:~:text=JavaScript's
%20interaction%20with%20HTML%20is,%2C%20resizing%20a%20window%2C%20etc.
https://www.geeksforgeeks.org/how-to-run-a-function-when-the-page-is-loaded-in-javascript/