Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
validation should be done without using any Validations controls
i mean form is design as

Textbox1(required Field validation)
textbox2(Email Validation)
Textbox3( Numeric validation)
Textbox4(NO special characters validation)
Textbox5(Alpha numeric validation)
Textbox6(Date validation)


here i need code using java script in asp.net without using any Validation controls to toolbox ........

and also give me information how to do in Asp.net
i have try lot but iam not getting solution where javascript code should write and where asp.net code should write. how it works...............



HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript">
<!--
    function Form1_Validator(theForm)
    
     {

         var alertsay = "";
         if (theForm1.Name.value == "") 
         {

             alert("You must enter an Name.");
             theForm.Name.focus();
             return (false);
   
         }
     
    }
</script>
<head runat="server">

    <title></title>

</head>

<body>
<form id="form1" runat="server">
<form action="javascript.asp?<%--ID=<%=siteID%>--%>"
method="POST" onsubmit="return Form1_Validator(this)" name="Form1">
<input type="submit" name="Submit" value="Submit">
    
  
        <asp:Label ID="Label1" runat="server" 
            style="z-index: 1; left: 42px; top: 291px; position: absolute" 
            Text="Label">
        <asp:TextBox ID="Name" runat="server" 
            style="z-index: 1; left: 96px; top: 287px; position: absolute">
    
        

     
  
   </form>
   </form>
</body>
</html>


i have written code like this but it not working
pls help me i am new for .Net.........
Posted
Updated 14-Nov-11 0:50am
v2
Comments
Smithers-Jones 14-Nov-11 7:27am    
Repost. Reported.
See: http://www.codeproject.com/Questions/283173/Need-Code-for-Validations-Using-Javascript-without

Take a peek at jQuery, which is able to help you validating user inputs : http://docs.jquery.com/Plugins/Validation[^]
 
Share this answer
 
the <script> block must be written eighter inside <head> or <body> tag, you have kept it in root, which is invalid, shift it to head, it will work.


mark as answer if solves your problem, it motivates :)...
 
Share this answer
 
v2
function checkEmpty()
{
if (document.getElementById("Textbox1").value=="")
{
alert("Please enter value in textbox1");
document.getElementById("Textbox1").focus();
return false;
}
}


function checkEmpty()
{

filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(document.getElementById("Textbox2").value))
alert ("valid")
else
alert ("Invalid")
}


function checkInt()
{ 
if (isNAN(document.getElementById("Textbox3").value))
alert("Text is alpha numeric, Please enter only numbers");
}

function checkSpecialchar()
{
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

for (var i = 0; i < document.getElementById("Textbox4").value.length; i++) 
{
if (iChars.indexOf(document.getElementById("Textbox4").value.charAt(i)) != -1) 
{
alert ("do not enter special char.");
return false;
}
}
}


function date()
{
var date_chk = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
if (! reDate.test(document.getElementById("Textbox6").value);) 
{
alert ("Invalid date");
return false;
}
}
 
Share this answer
 
hi,
please check this code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">   
 <title></title>     
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />     
<script type="text/javascript">
function ValidateMobileNumber() 
{            
var x = document.getElementById('TextBox1').value;             
if (isNaN(x) || x.indexOf(" ") != -1) 
{                 
alert("Enter numeric value"); return false;             
}            
 if (x.length != 11) 
{                 
alert("enter 11 numbers"); 
return false;             }            
 }     
</script>
</head>
<body>    
<form id="form1"  runat="server">          
 <div>               
<asp:TextBox id="TextBox1" runat="server" />               
<br />            
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" OnClientClick="javascript:ValidateMobileNumber();" />       
 </div>    
</form>
</body>
</html>
 
Share this answer
 
v3
Comments
Raghu16 14-Nov-11 7:17am    
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" OnClientClick="javascript:ValidateMobileNumber();" />

here it is getting error
mandarapu 14-Nov-11 7:21am    
hi,
here just call like this
<asp:Button ------ OnClientClick="return ValidateMobileNumber()"
javascript
function checkfname(elem)
 {
    var alphaExp = /[^a-zA-Z]/;
    if (elem.value.length == 0 || alphaExp.test(elem.value)) {
        alert("Firstname should not be blank and only characters");
        elem.focus();
        return false;
    }
    return true;
}

function checklname(elem)
 {
    var alphaExp = /[^a-zA-Z]/;
    if (elem.value.length == 0 || alphaExp.test(elem.value)) 
    {
        alert("Lastname should not be blank and only characters");
        elem.focus();
        return false;
    }
    return true;
}
function validatenum(elem)
 {
    if (isNaN(elem.value) || (elem.value.length!=10))
     {

        alert("Contact Number should be number and ten characters only");
        return false;
        elem.focus();
    }
    return true;
}
function validateadd(elem)
{
   if (elem.value.length == 0)
    {
        alert("Address field Can't be left blank");
        elem.focus();
        return false;
    }
    return true;
  }
 function validateemail(elem)
   {
       var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
       if (emailExp.test(elem.value)) 
      {
          return true;
      }
      else
       {
          alert("Not a valid Email Format");
          elem.focus();
          return false;
      }
  }
function checkuname(elem) 
  {
      var alphaExp = /[^a-zA-Z]/;
      if (elem.value.length == 0 || alphaExp.test(elem.value)) 
      {
          alert("Username should not be blank and only characters");
          elem.focus();
          return false;
      }
      return true;

  }

  function checkpass(elem)
   {
       if (isNaN(elem.value) || elem.value.length == 0)
           {
              alert("Enter only Numbers and can't be left blank");
              return false;
           }
           return true;
  }
function conpass(elem,elem1) {
       if (elem1.value != elem.value || (isNaN(elem1.value)) || elem1.value.length ==0 )
       {
           alert("Password doesn't match");
           return false;
       }
       return true;
  }

function checkall()
 {
    var firstname = document.getElementById("fname");
    var lastname = document.getElementById("lname");
    var num = document.getElementById("number");
    var add = document.getElementById("address");
    var email = document.getElementById("email");
    var uname = document.getElementById("uname");
    var password = document.getElementById("pwd");
    var cpassword = document.getElementById("cpwd");
    if (checkfname(firstname)) {
        if (checklname(lastname)) {
            if (validatenum(num)) {
                if (validateadd(add)) {
                    if (validateemail(email)) {
                        if (checkuname(uname)) {
                            if (checkpass(password)) {
                                if (conpass(password, cpassword)) {
                                    return true;
                                }
                            }
                        }  
                    }

                }
            }
        }
    }
    return false;
}

file in the aspx page:
   <table class="newusertable" align="center">
        <tr>
				 		<td class="tbox" valign="top">
						
						First Name:</td>
						<td class="style2">
                            <asp:textbox class="txtbkcolor" id="fname" runat="server" width="160px" xmlns:asp="#unknown"></asp:textbox>
                            </td>
						</tr>

						<tr>
 						<td class="tbox" valign="top">
						
						Last Name:</td>
						<td class="style2">
                            <asp:textbox class="txtbkcolor" id="lname" runat="server" width="171px" xmlns:asp="#unknown"></asp:textbox>
                            </td>
						</tr>

						<tr>
 						<td class="tbox" valign="top">
						
						Gender:</td>
						<td class="tbox">
						    <asp:radiobutton id="rbmale" class="tbox" runat="server" checked="True" groupname="a" xmlns:asp="#unknown">
                                Text="Male" />
                            <asp:radiobutton id="rbfemale" class="tbox" runat="server" groupname="a" text="Female" />
 </asp:radiobutton></td>
						</tr>

						<tr>
 						<td class="tbox" valign="top">
						
						Phone No.:</td>
  						<td class="tbox">
                            <asp:textbox class="txtbkcolor" id="number" runat="server" width="170px" xmlns:asp="#unknown"></asp:textbox>
                            </td>
						</tr>

						 <tr>
 						<td class="tbox" valign="top">
						
						Address:</td>
 						<td class="style1">
                            <asp:textbox class="txtbkcolor" id="address" runat="server" textmode="MultiLine" xmlns:asp="#unknown"></asp:textbox>
                             </td>
						</tr>

<tr>
 <td class="tbox">

Email ID</td>
  <td class="style2">
      <asp:textbox class="txtbkcolor" id="email" runat="server" width="184px" xmlns:asp="#unknown"></asp:textbox>
    </td>
</tr>

 <tr>
  <td class="tbox" valign="top">
 

      UserName:</td>
  <td class="style2">
      <asp:textbox class="txtbkcolor" id="uname" runat="server" width="179px" xmlns:asp="#unknown"></asp:textbox>
     </td></tr>


  <tr>
  <td class="tbox" valign="top">
 
      Password:</td>
  <td class="style2">
      <asp:textbox class="txtbkcolor" id="pwd" runat="server" width="179px" xmlns:asp="#unknown">
          TextMode="Password"></asp:textbox>
     </td></tr>



 <tr>
  <td class="tbox" valign="top">
 
    Confirm  Password:</td>
  <td class="style2">
      <asp:textbox class="txtbkcolor" id="cpwd" runat="server" width="179px" xmlns:asp="#unknown">
          TextMode="Password"></asp:textbox>
     </td></tr>

     <tr>
  <td class="tbox" valign="top">
 
    Security Question:</td>
  <td class="style2">
      <asp:textbox class="txtbkcolor" id="secques" runat="server" width="179px" xmlns:asp="#unknown"></asp:textbox>
     </td></tr>

     <tr>
  <td class="tbox" valign="top">
 
    Answer:</td>
  <td class="style2">
      <asp:textbox class="txtbkcolor" id="secans" runat="server" width="179px" xmlns:asp="#unknown"></asp:textbox>
     </td></tr>



 


  <tr align="center">
<td colspan="2">
    <asp:button class="but" id="submit" runat="server" text="Submit" onclientclick="return checkall();" xmlns:asp="#unknown">
        onclick="submit_Click" Font-Bold="False" 
        Font-Size="Large"/>
       <asp:button id="but_home" runat="server" class="but" font-bold="False">
                                                Font-Size="Medium" onclick="but_home_Click" 
                                                Text="Login" Height="32px" 
        Width="65px" />
                                                
    <asp:button class="but" id="cancel" runat="server" text="Cancel">
        onclick="cancel_Click" Font-Bold="False" Font-Size="Medium" Height="31px" 
        Width="79px" />
       </asp:button></asp:button></asp:button></td>
</tr>
        </table>

For calling the javascript file in aspx page add the reference in head.
<head runat="server">
    <title></title>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <link href="CSS/createuser.css" rel="stylesheet" type="text/css" />
    <script src="JS/NewUser.js" type="text/javascript"></script>

    <style type="text/css">
        .style1
        {
            width: 483px;
        }
    </style>

</head></head>
 
Share this answer
 
Comments
Raghu16 14-Nov-11 7:21am    
Where should i write javascripting code And Asp.net code . i mean whether i should be in between <head> </head>
or <script></script> or <body></body> or <form></form>
pls give be some information because i didnot have any touch in dotnet

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900