Solved

ASP.NET - Javascript to validate text boxes

Posted on 2012-03-21
10
244 Views
Last Modified: 2012-04-12
I have an ASP.NET app using masterpages - I've attached a sample of the code.  I tried using the Validate controls with .NET but don't like how they are working so I want to implement javascript to perform the validations.

1.  Need to ensure that no text boxes are empty
2.  NewPassword and ConfirmPassword match
3.  OldPassword matches OrigPassword (I also have this in a session).

Any ideas?
ASPX-Page---Need-javascript-to-v.txt
0
Comment
Question by:CipherIS
  • 5
  • 5
10 Comments
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
CASE 1 : make sure that no textbox should be empty .
CASE 2 :  NewPassword and ConfirmPassword match

you can try this

$(".submitButton").click(function() {
  var isValid = true;
$.each($("input[type=text]"),function() {
     if($(this).val() == "")
    {
     alert("No text box can be empty");
     $(this).focus();
     isValid =false;
    }
    if(!isValid ) return false;
});
 if(isValid)
 {
    if($("#txtNewPwd").val() != $("#txtConfirmPwd").val())
    {
           alert("New Password and Confirm Password must match.");
           isValid = false;
    }
 }
  return isValid
});



CASE 3 :  OldPassword matches OrigPassword (I also have this in a session).

For this,you have to make an jQuery ajax call to ther server to validate by comparing the oldpassword from the password stored in the session,as session will not be accessible to the client code (i.e jquery),So you have to go to the server by doing asynchronus call i.e Ajax call.
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
Code is not working.  I receive $ is not defined.

<script type="text/javascript">

$(".submitButton").click(function() {
  var isValid = true;
$.each($("input[type=text]"),function() {
     if($(this).val() == "")
    {
     alert("No text box can be empty");
     $(this).focus();
     isValid =false;
    }
    if(!isValid ) return false;
});
 if(isValid)
 {
    if($("#txtNewPwd").val() != $("#txtConfirmPwd").val())
    {
           alert("New Password and Confirm Password must match.");
           isValid = false;
    }
 }
  return isValid
});

</script>
0
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
you need to add the Jquery reference on the head section of your page. Just add the line

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

in the head section of the page.

OR
you can also get the local copy of jquery from the jquery.com site.

Both of them will work.
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
so basically this is the solution?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">

$(".submitButton").click(function() {
  var isValid = true;
$.each($("input[type=text]"),function() {
     if($(this).val() == "")
    {
     alert("No text box can be empty");
     $(this).focus();
     isValid =false;
    }
    if(!isValid ) return false;
});
 if(isValid)
 {
    if($("#txtNewPwd").val() != $("#txtConfirmPwd").val())
    {
           alert("New Password and Confirm Password must match.");
           isValid = false;
    }
 }
  return isValid
});

</script>
0
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
Yes.This will help you to achieve what you want.Keep the Script to the head section of your page.If any issue persists then share that.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:CipherIS
Comment Utility
I'm using master pages - I can't put it in the head section of my page as the page does not have a head section.

Please look at the document that I attached with my posting.
0
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
Ok  you can place  the following line (script> in the Head section of your Master Page. It will have no issue.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
and the script to validate?  put that in my page?
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
Comment Utility
yes,in your page (content page),
place the following line anywhere in your page.

<script Language="Javascript">
    $(function() {

$(".submitButton").click(function() {
  var isValid = true;
$.each($("input[type=text]"),function() {
     if($(this).val() == "")
    {
     alert("No text box can be empty");
     $(this).focus();
     isValid =false;
    }
    if(!isValid ) return false;
});
 if(isValid)
 {
    if($("#txtNewPwd").val() != $("#txtConfirmPwd").val())
    {
           alert("New Password and Confirm Password must match.");
           isValid = false;
    }
 }
  return isValid
});


    });
</script>

Hope Now it will fix up your problem.
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
will test soon - been tied up
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now