Log in

View Full Version : jQuery validation



reisve
06-21-2013, 10:21 PM
Hi

I'm trying to validate a few forms with jQuery but none works, even if at least one of them was working before. Here is the code:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo $title['title']; ?></title>
<base href="http://root.com/divdev/">
<meta http-equiv="Content-Type" content="text/html; charset=Western (ISO-8895-1)" />
<link rel="stylesheet" type="text/css" href="css/frontend.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.validate.pack.js" type="text/javascript"></script>

<script type="text/javascript">
// When the browser is ready...
$(function() {

// Setup form validation on the #contactform element
$("#contactform").validate({

// Specify the validation rules
rules: {
contactname: "required",
email: "required",
subject: "required",
message: "required"
},

// Specify the validation error messages
messages: {
contactname: "É obrigatório o preenchimento do seu nome",
email: "É obrigatório o preenchimento do seu email",
subject: "É obrigatório o preenchimento do assunto da mensagem",
message: "É obrigatório o preenchimento da mensagem"
),

submitHandler: function(form) {
form.submit();
}

});
});
</script>

<script>
$('#msg').ready(function () {
setTimeout(function () {
$('#msg').hide();
window.location.href = goto;
}, 3000);
});
});
</script>

<script>
// When the browser is ready...
$(function() {

// Setup form validation on the #addlinkform element
$("#addlinkform").validate({

// Specify the validation rules
rules: {
category: "required",
company: "required",
description: "required",
address: "required",
county: "required",
city: "required",
state: "required",
country: "required",
phone: "required",
email: "required",
duration: "required"
},

// Specify the validation error messages
messages: {
category: "<?php echo $addlinkinfo['category']; ?>",
company: "<?php echo $addlinkinfo['company']; ?>",
email: "<?php echo $addlinkinfo['email']; ?>",
description: "<?php echo $addlinkinfo['description']; ?>",
address: "<?php echo $addlinkinfo['address']; ?>",
county: "<?php echo $addlinkinfo['county']; ?>",
city: "<?php echo $addlinkinfo['city']; ?>",
state: "<?php echo $addlinkinfo['state']; ?>",
country: "<?php echo $addlinkinfo['country']; ?>",
duration: "<?php echo $addlinkinfo['duration']; ?>"
},

submitHandler: function(form) {
form.submit();
}
});
});
</script>

<script>
// When the browser is ready...
$(function() {

// Setup form validation on the #registerform element
$("#registerform").validate({

// Specify the validation rules
rules: {
fname: "required",
lname: "required",
email: {
required: true,
email: true,
},
user: {
required: true,
},
pass1: {
required: true,
},
pass2: {
required: true,
equalTo: "#pass1",
}
},

// Specify the validation error messages
messages: {
fname: "<?php echo $regerror['fname']; ?>",
lname: "<?php echo $regerror['lname']; ?>",
email: {
required: "<?php echo $regerror['email']; ?>",
email: "<?php echo $regerror['valemail']; ?>",
},
user: {
required: "<?php echo $regerror['user']; ?>",
},
pass1: {
required: "<?php echo $regerror['pass1']; ?>",
},
pass2: {
required: "<?php echo $regerror['pass2']; ?>",
equalTo: "<?php echo $regerror['difpass']; ?>",
},
},

submitHandler: function(form) {
form.submit();
}
});
});
</script>

<script>
// Setup form validation on the #loginform element
$("#loginform").validate({

// Specify the validation rules
rules: {
user: "required",
pass: "required"
},

// Specify the validation error messages
messages: {
user: "<?php echo $logerror['user']; ?>",
pass: "<?php echo $logerror['pass']; ?>"
},

submitHandler: function(form) {
form.submit();
}
});
});
</script>
</head>
<body>


I keep all the validation code in the header seccion.

Can any one help me please with this?

Beverleyh
06-21-2013, 11:18 PM
Part of the problem could be that you're linking to 2 jQuery libraries and 2 validation scripts, which are probably conflicting.

I'm not familiar with a jQuery validation pack/plugin but the scripts look a little 'off'. Your setup goes;
- jQuery 1.9.1
- validation pack/plugin
- jQuery 1.3.2
- validation pack/plugin
- JavaScript call + validation specifics

Whereas I'd expect to see;
- jQuery 1.9.1
- validation pack/plugin
- JavaScript call + validation specifics

Without any documentation, or a complete web page with form markup, all I can suggest is that you only use one version of the jQuery library and one version of the validation pack/plugin, with the JavaScript call below those. Use whichever very of the jQuery library is recommended for the validation plugin while troubleshooting, and then maybe try more recent ones while testing to ensure that library upgrades still work with that particular plugin.

If you need more help, please provide links to the validation plugin source/documentation and a live, complete web page.

reisve
06-22-2013, 09:57 AM
Thank You. I don't know much about jQuery (and JavaScript), and that code was grabbed from the net, an I used it as I found it (with a couple of changes in the code to make it more readable. I hope it will solve the issue.

Thank You

Beverleyh
06-22-2013, 01:56 PM
I'm not sure what you expect us to do with such little information.

You'll need to play detective and do some more research to provide us with the tools/knowledge to help you. At the moment, you haven't given us enough information to work with.

Thoughts to get you started;

- Revisit the website where you copied the code (you might need to retrace you steps to find it again but look for references to source websites and/or authors).

- Look inside the validation plugin files for author/source website information, then visit the website to look for installation instructions and/or do a separate search on the author name and follow whatever links you discover to see if you can find a trail to suitable documentation.

- Search on key lines of code from the JavaScript call (you might pull up related tutorials or documentation)

You should be able to find more information if you look hard enough - appropriate documentation is the key here and even people who are comfortable with JavaScript appreciate a little guidance to familiarise themselves with somebody else's work.

Lastly, when you post your findings, you should also provide all the source code for your website by way of an actual live link that utilises the validation plugin you've found. The JavaScript is referencing form fields that we, so far, know nothing about - we can't guess usage and implementation for you.

reisve
06-22-2013, 02:06 PM
It is solved. Thank You

getitfaster
06-22-2013, 05:45 PM
I think the issue was this bit $(function() at the start of where you first embedded js goes. It should be $(function(dom).ready(){

jscheuer1
06-23-2013, 07:05 AM
I think the issue was this bit $(function() at the start of where you first embedded js goes. It should be $(function(dom).ready(){

As far as I know, $(function(dom).ready() is not valid javascript nor valid jQuery. The:


$(function(){
//code to execute on DOM ready
});

is valid jQuery shorthand for jQuery's:


$(document).ready(function(){
//code to execute on DOM ready
});

See also:

http://api.jquery.com/ready/