Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created 2pages login and signup. On signup page i am doing validations using javascript.

XML
<head  runat="server">
    <script type="text/javascript">
        function Validateform() {
            var msg = "";



            if (document.getElementById("txtfirst").valueOf == "") {
                msg += "Enter Firstname\n";
            }
            else {
                var ex = /^[a-zA-Z]/;
                if (ex.test(document.getElementById("txtfirst").valueOf == false)) {
                    msg += "please enter only characters in first name\n";
                }
            }
</script>
 if (msg == "") {
                return true;
            }
            else {
                alert(msg);
                return false;
            }

 <td colspan="2">
               <asp:Button ID="signupbtn" runat="server" Text="Sign Up" onclick="signupbtn_Click" OnClientClick="return Validateform();" Class="button" />
                <asp:Button ID="cancelbtn" runat="server" Text="Cancel" onclick="cancelbtn_Click" Class="button" />
        </td>


But it is not working properly. The problem is that when i enter correct data it is still giving me validation error massages. and also validation for non empty field is not at all working.
please help. Its urgent.Thank you in advance
Posted
Updated 16-Jan-14 22:53pm
v3
Comments
Tom Marvolo Riddle 17-Jan-14 2:58am    
Post your relevant code snippet
Member 10523130 17-Jan-14 3:28am    
here is my signup.aspx file code in which i have written javascript

<head runat="server">
<script type="text/javascript">
function Validateform() {
var msg = "";



if (document.getElementById("txtfirst").valueOf == "") {
msg += "Enter Firstname\n";
}
else {
var ex = /^[a-zA-Z]/;
if (ex.test(document.getElementById("txtfirst").valueOf == false)) {
msg += "please enter only characters in first name\n";
}
}
</script>
if (msg == "") {
return true;
}
else {
alert(msg);
return false;
}

<td colspan="2">
   <asp:Button ID="signupbtn" runat="server" Text="Sign Up" onclick="signupbtn_Click" OnClientClick="return Validateform();" Class="button" />
    <asp:Button ID="cancelbtn" runat="server" Text="Cancel" onclick="cancelbtn_Click" Class="button" />
</td>
Deviprasad Das 17-Jan-14 3:14am    
Please post some piece of your code.

Which fields do you need to validate in Signup form?Please explain.

You can refer below links for validation of signup form in javascript.
http://www.w3resource.com/javascript/form/javascript-sample-registration-form-validation.php[^]
http://www.scriptiny.com/2012/12/register-or-signup-form-validation-using-javascript/[^]

This may help.
 
Share this answer
 
Comments
Member 10523130 17-Jan-14 3:36am    
I need to validate firstname,middlename,surname,contact no,date,also i need to validate duplicate username.
the code given on link w3source is different than what i have used.I am not getting it.It is complicated.is there any other solution?
pls help
Gitanjali Singh 17-Jan-14 3:44am    
Try like this. this one is simple.
function validateForm()
{
var Firstname = document.getElementById("txtFname").value;
var Lastname = document.getElementById("txtLname").value;
var Username = document.getElementById("txtUserName").value;
var e = document.getElementById("ddlUserRole");
var Role = e.selectedIndex;
var currentProject =document.getElementById("lbCurrentProject").value;
var Password = document.getElementById("txtPassword").value;
var Confirmpassword = document.getElementById("txtConfirmPassword").value;


if (Firstname == "First Name" || Firstname == "")
{
alert("Firstname must be filled out");
return false;
}


if (Lastname == "Last Name" || Lastname == "")
{
alert("Lastname must be filled out");
return false;
}


if (Username == "User Name" || Username == "")
{
alert("UserName must be filled out");
return false;
}

if (Role == 0 || Role == "")
{
alert("Select your role");
return false;
}

if (currentProject == null || currentProject == "")
{
alert("Select your current projects");
return false;
}

if (Password == null || Password == "")
{
alert("Password must be filled out");
return false;
}
if (Confirmpassword == null || Confirmpassword == "")
{
alert("Confirmpassword must be filled out");
return false;
}

if (Password != Confirmpassword)
{
alert(" Passwords must match");
return false;
}

//alert("You are registered successfully!!!!! Login Now....");
return true;
}
Member 10523130 17-Jan-14 4:26am    
hey thanx it worked fine for empty fields. But now I have to check for Only characters in firstname,middlename and etc..how shud i check?pls help
Gitanjali Singh 17-Jan-14 4:42am    
Use this function
function IsNumericOrCharacter(strString)
{
//valid string
var strValidChars = "0123456789.-~`@#$%^&*(){}[]:;'<>? ";
var strChar;
var returnResult = true;

if (strString.length == 0) return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && returnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) != -1)
{
//document.getElementById('+ id +').value="";
alert("Numeric or special characters or spaces are not allowed");
returnResult = true;
}
}

return returnResult;
}
Member 10523130 17-Jan-14 4:51am    
but where shud i call this function???means the above validation function i have called on onClientClick..this wiil be the second function so where shud i call?
Use
JavaScript
if (document.getElementById("txtfirst").value == "") 


instead of

JavaScript
if (document.getElementById("txtfirst").valueOf == "")
 
Share this answer
 
Comments
Member 10523130 17-Jan-14 4:27am    
i tried it is not working.. ;(

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