form validation

i need a simple form validator
it must be able to be called with a click event

thanks
LVL 25
dgrafxAsked:
Who is Participating?
 
darren-w-Connect With a Mentor Commented:
this works:
<!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>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>title</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
       <script type="text/javascript" src="validate.js"></script>

      <script type="text/javascript">
         $(function(){
            $(".submit").live("click", function() {
              // console.log("got here");
               $(this).parent("form").validate().form();
            });
         });
      </script>
   </head>
   <body >
      <form method="get" action="">
         <input id="tester" name="tester" class="required" minlength="2" /> <br />
            <a href="#" class="submit" onclick="return false;">Save</a>
            <!--- <input class="submit" type="submit" value="Submit"/>  --->
      </form>
   </body>
</html>

Open in new window

0
 
darren-w-Commented:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/

can help with your script if required...
0
 
dgrafxAuthor Commented:
this script says not yet compatible with 1.5
i am on 1.4 but I know others where I work are on 1.5
so if and when they need to use it - it will break

what are your comments on this?
thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
darren-w-Commented:
I've been using it successfully with the latest version of 1.5, with no major flaws, what I would say is to test your pages rigorously, there is a compatable version here:https://github.com/jzaefferer/jquery-validation:

off page: Please note: The latest release isn’t, yet, compatible with jQuery 1.5.x. You can find a compatible version in the GitHub repository.
0
 
dgrafxAuthor Commented:
sounds good - I'll try it
so do you have some on page code (example)?
0
 
ccwill88Commented:
0
 
dgrafxAuthor Commented:
ccwill - i like it but that wont fly here

need something real simple

looking for some example code guys
where you click and all fields then validated would be ideal

thanks
0
 
darren-w-Commented:
Here are some demo's:

http://jquery.bassistance.de/validate/demo/

with further examples at the bottom of this page

0
 
dgrafxAuthor Commented:
ok - to clarify a bit more ...
I did stumble onto that page and tried to use the demo found there ...
I failed - didn't work.
I don't see what it is really doing ???
is it triggered by form submit?
I need it to be triggered by a click of a link that is not directly calling a submit

perhaps you can shed some light
I have stripped down the example to virtually nothing and it works great
but as i said - i am not calling the form submit if that is what is happening

thanks for your time
0
 
darren-w-Commented:
Ok,

In the example below, the validate method gets the requirements for the form validation from the class and minlength setting in the input definition.

<html>
<head>
<title>a title</title>
<script src="../lib/jquery.js" type="text/javascript"></script>
<script src="../jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("form").validate();
})
</script>
</head>
<body>
<form method="get" action="">
<input id="name" name="name" class="required" minlength="2" />
<input class="submit" type="submit" value="Submit"/>
</form>
</body>
</html>

alternatively you could omit the settings in the body (eg: name="name" class="required) and put it in the script like so:

$(function(){
$("form").validate(
rules: name: {
                        required: true,
                        minlength: 2
                  });
}),

the validate method detects the submit press and displays a default  error,

you can also give custom messages
Carrying on from above

messages: username: {
                        required: "Please enter your name",
                        minlength: "Your name must consist of at least 2 characters"
                  }

If you take a look in the source it gives examples with other form elements.

If you want to change the way the errors are displayed you can override the styles for text.


Darren
0
 
dgrafxAuthor Commented:
run the below code to see what i'm talking about

=====================
<script type="text/javascript">
$(".submit").live("click", function() {
alert();
$("form").validate();
});
</script>

<form method="get" action="">
<input id="tester" name="tester" class="required" minlength="2" /> <br>
<a href="" class="submit" onclick="return false;">Save</a>
<!--- <input class="submit" type="submit" value="Submit"/>  --->
</form>
=====================

I am NOT calling a form submit directly
I'm doing some ajax stuff
I need the fields to be validated when called by jquery - NOT from a form submit
do you know what i'm talking about?
0
 
dgrafxAuthor Commented:
right on buddy - looks good!
thanks ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.