Skip to content

Валидација

Email validation:

ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

Пример са часа

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upitnik.aspx.cs" Inherits="ValidacijaElemenataNaFormi.Upitnik" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <!--- POLJE ZA IME (obavezno) --->
            Ime: 
            <asp:TextBox ID="txtIme"
                ValidationGroup="Group1"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtImeValidator"
                ErrorMessage="Ime je obavezno!"
                ControlToValidate="txtIme"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server"></asp:RequiredFieldValidator>
            <br />

            <!--- POLJE ZA EMAIL (obavezno + format email adrese) --->
            Email: 
            <asp:TextBox ID="txtEmail"
                ValidationGroup="Group1"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtEmailValidator"
                ErrorMessage="Email je obavezan!"
                ControlToValidate="txtEmail"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="txtEmailExpression"
                ErrorMessage="Email nije u ispravnom formatu"
                ControlToValidate="txtEmail"                
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server"></asp:RegularExpressionValidator>
            <br />

            <!--- POLJE ZA LOZINKU (obavezno) --->            
            Password: 
            <asp:TextBox ID="txtPass"
                ValidationGroup="Group1"
                TextMode="Password"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtPassValidator"
                ErrorMessage="Password je obavezan!"
                ControlToValidate="txtPass"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server"></asp:RequiredFieldValidator>
            <br />

            <!--- POLJE ZA POTVRDU LOZINKE (obavezno + poredjenje sa prethodnim) --->            
            Confirm Password: 
            <asp:TextBox ID="txtConfirm"
                ValidationGroup="Group1"
                TextMode="Password"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtConfirmValidator"
                ErrorMessage="Obavezno je ponovno ukucati password!"
                ControlToValidate="txtConfirm"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server"></asp:RequiredFieldValidator>
            <asp:CompareValidator
                ID="PorediLozinkeValidator"
                ErrorMessage="Lozinke se ne poklapaju"
                ControlToCompare="txtConfirm"
                ControlToValidate="txtPass"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server" ></asp:CompareValidator>
            <br />

            <!--- POLJE ZA BROJ GODINA (obavezno + u zadatom opsegu) --->            
            Age: 
            <asp:TextBox ID="txtAge"
                ValidationGroup="Group1"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtAgeValidator"
                ErrorMessage="Obavezno je uneti broj godina!"
                ControlToValidate="txtAge"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                runat="server"></asp:RequiredFieldValidator>
            <asp:RangeValidator ID="GodineValidator"
                ErrorMessage="Nedozvoljen broj godina!"
                ControlToValidate="txtAge"
                Display="Static"
                ForeColor="Red"
                SetFocusOnError="True"
                ValidationGroup="Group1"
                EnableClientScript="False"
                MinimumValue="14"
                MaximumValue="19"
                Type="Integer"
                runat="server"></asp:RangeValidator>
            <br />
            <asp:Button ID="btnSubmit"
                ValidationGroup="Group1"
                CausesValidation="true"
                OnClick="btnSubmit_Click"
                Text="SUBMIT"
                runat="server"/>
            <br />
            <asp:Label ID="lblPoruka"
                runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

+ CS за испис порука у лабели испод дугмета

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ValidacijaElemenataNaFormi
{
    public partial class Upitnik : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtImeValidator.IsValid && txtEmailValidator.IsValid
                && txtEmailExpression.IsValid && txtPassValidator.IsValid &&
                txtConfirmValidator.IsValid && PorediLozinkeValidator.IsValid &&
                txtAgeValidator.IsValid && GodineValidator.IsValid)
                lblPoruka.Text = "Ispravno su popunjena sva polja.";
            else
                lblPoruka.Text = "Neka polja nisu ipravno popunjena!";
        }
    }
}

Пример 1

aspx

    <form id="form1" runat="server">
        <div>
            Ime
            <asp:TextBox ID="txtIme"
                ValidationGroup="Group1"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtImeValidator"
                ControlToValidate="txtIme" 
                Display="Static"
                ErrorMessage="Ovo polje je obavezno!" 
                EnableClientScript="False"
                ValidationGroup="Group1"
                ForeColor="Red"
                SetFocusOnError="True"
                runat="server"></asp:RequiredFieldValidator>
            <br />
            <asp:Button ID="btnValidacija"
                ValidationGroup="Group1"
                CausesValidation="true"
                Text="SUBMIT"
                OnClick="btnValidacija_Click"
                runat="server"/>
            <br />
            <asp:Label ID="lblPoruka"
                ForeColor="Red"
                runat="server"></asp:Label>
        </div>
    </form>

aspx.cs

        protected void btnValidacija_Click(object sender, EventArgs e)
        {
            if (txtImeValidator.IsValid)
                lblPoruka.Text = "Validacija: OK";
            else
                lblPoruka.Text = "Validacija: Moraš popuniti sva polja!";
        }

Пример 2

aspx

            Prezime
            <asp:TextBox ID="txtPrezime"
                AutoPostBack="true" 
                ValidationGroup="Group2"
                CausesValidation="true"
                runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="txtPrezimeValidator"
                ValidationGroup="Group2"
                ControlToValidate="txtPrezime"
                ErrorMessage="Ovo polje je obavezno!"
                EnableClientScript="False"
                ForeColor="Red"
                SetFocusOnError="True"
                runat="server"></asp:RequiredFieldValidator>
            <br />
            <asp:Button ID="btnValidacija2"
                ValidationGroup="Group2"
                CausesValidation="true"
                Text="SUBMIT"
                OnClick="btnValidacija2_Click"
                runat="server"/>
            <br />
            <asp:Label ID="lblPoruka2"
                ForeColor="Red"
                runat="server"></asp:Label>
            <br />

aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack && Context.Request.Form["__EVENTTARGET"] == "txtPrezime")
            {
                Validate("Group2");
                if (Page.IsValid)
                    lblPoruka2.Text = "Validacija: OK";
                else
                    lblPoruka2.Text = "Validacija: Moraš popuniti sva polja!";
            }
        }

        protected void btnValidacija2_Click(object sender, EventArgs e)
        {
            Validate();
            if (Page.IsValid)
                lblPoruka2.Text = "Validacija: OK";
            else
                lblPoruka2.Text = "Validacija: Moraš popuniti sva polja!";
        }