Solved

form validation

Posted on 2011-03-11
12
321 Views
Last Modified: 2012-06-27
i need a simple form validator
it must be able to be called with a click event

thanks
0
Comment
Question by:dgrafx
  • 6
  • 5
12 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 35109582
http://bassistance.de/jquery-plugins/jquery-plugin-validation/

can help with your script if required...
0
 
LVL 24

Author Comment

by:dgrafx
ID: 35109688
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
 
LVL 13

Expert Comment

by:darren-w-
ID: 35109748
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
 
LVL 24

Author Comment

by:dgrafx
ID: 35109779
sounds good - I'll try it
so do you have some on page code (example)?
0
 
LVL 3

Expert Comment

by:ccwill88
ID: 35109785
0
 
LVL 24

Author Comment

by:dgrafx
ID: 35110343
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:darren-w-
ID: 35110500
Here are some demo's:

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

with further examples at the bottom of this page

0
 
LVL 24

Author Comment

by:dgrafx
ID: 35110581
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
 
LVL 13

Expert Comment

by:darren-w-
ID: 35110821
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
 
LVL 24

Author Comment

by:dgrafx
ID: 35110908
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
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 35111245
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
 
LVL 24

Author Comment

by:dgrafx
ID: 35111300
right on buddy - looks good!
thanks ...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now