?
Solved

form validation

Posted on 2011-03-11
12
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 25

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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 25

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 25

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
 
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 25

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 25

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 2000 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 25

Author Comment

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

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)

743 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