I have a registration form and several codes for validation but i can't join them.

regsitration form:

    include 'topo.php';
    <!-- content -->
    <div class="indent">
    <h2> Inscrição </h2>
    <form action="envia.php" method="post" onsubmit="return validar()" >
    <legend> ALUNO </legend>
    <div class="field"><label>Username*: </label><input type="text" size="30" name = "username"/></div>
    <div class="field"><label>Password*: </label><input type="password" size="25" name = "pass"/></div>
    <div class="field"><label>Confirma Password*: </label><input type="password" size="25" name = "conf_pass"/></div>
    <div class="field"><label>Nome*: </label><input type="text" size="40" name = "nome"/></div>
    <div class="field"><label>Data de Nascimento: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="3" maxlength="2" name = "dia"/> / <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="3" maxlength="2" name = "mes"/> / <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "ano"/> (DD/MM/AAAA)</div>
    <div class="field"><label>Morada: </label><input type="text" onkeypress="return onlyNumbers();" size="39" name = "morada"/></div>
    <div class="field"><label>Código Postal: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "cp1"/> - <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="4" maxlength="3" name = "cp2"/></div>
    <div class="field"><label>Telemóvel*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="10" maxlength="9" name = "tele"/></div>
    Curso*: <select name="curso">
    <option value = 1> Piano </option>
    <option value = 2> Orgão </option>
    <option value = 3> Guitarra Elétrica </option>
    <option value = 4> Guitarra Acústica </option>
    <option value = 5 Violino </option>
    <option value = 6> Canto </option>
    <option value = 7> Bateria </option>
    <option value = 8> Saxofone </option>
    <option value = 9> Flauta </option>
    <option value = 10> Baixo </option>
    <option value = 11> Violoncelo </option>
    <div class="field"><label>E-Mail*: </label><input type="text" value="" name = "email" size = "25"/></div>
    <textarea cols="30" rows="7" name="obs"></textarea>
    <legend> ENCARREGADO DE EDUCAÇÃO </legend>
    <div class="field"><label>Nome*: </label><input type="text" size="40" name = "nome_ee"/></div>
    <div class="field"><label>Morada: </label><input type="text" size="39" name = "morada_ee"/></div>
    <div class="field"><label>Código Postal: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "cp1_ee"/> - <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="4" maxlength="3" name = "cp2_ee"/></div>
    <div class="field">Telemóvel*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="10" maxlength="9" name = "tele_ee"/></div>
    <div class="field"><label>E-Mail*: </label><input type="text" value="" name = "email_ee" size = "25"/></div>
    * Campos Obrigatórios
    <input type="submit" value="Enviar">
    <input type="Reset" value="Apagar">
    include 'rodape.php';

This form i downloaded because of the validation:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="web design, website development, web application development, print design, database development, ecommerce websites, search engine optimization, packaging design" />
<meta name="description" content="Tutorial demo showing how to add jquery animated validation to any form" />
<meta name="robots" content="all" />
<title>jQuery Form Validation With Animated Error Messages Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/JavaScript">
$(document).ready(function() { 

		// Declare the function variables:
		// Parent form, form URL, email regex and the error HTML
		var $formId = $(this).parents('form');
		var formAction = $formId.attr('action');
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var $error = $('<span class="error"></span>');

		// Prepare the form for validation - remove previous errors

		// Validate all inputs with the class "required"
			var inputVal = $(this).val();
			var $parentTag = $(this).parent();
			if(inputVal == ''){
				$parentTag.addClass('error').append($error.clone().text('Required Field'));
			// Run the email validation using the regex for those input items also having class "email"
			if($(this).hasClass('email') == true){
					$parentTag.addClass('error').append($error.clone().text('Enter valid email'));
			// Check passwords match for inputs with class "password"
			if($(this).hasClass('password') == true){
				var password1 = $('#password-1').val();
				var password2 = $('#password-2').val();
				if(password1 != password2){
				$parentTag.addClass('error').append($error.clone().text('Passwords must match'));

		// All validation complete - Check if any errors exist
		// If has errors
		if ($('span.error').length > 0) {
				// Set the distance for the error animation
				var distance = 5;
				// Get the error dimensions
				var width = $(this).outerWidth();
				// Calculate starting position
				var start = width + distance;
				// Set the initial CSS
					display: 'block',
					opacity: 0,
					right: -start+'px'
				// Animate the error message
					right: -width+'px',
					opacity: 1
				}, 'slow');
		} else {
		// Prevent form submission
	// Fade out error message when input field gains focus
		var $parent = $(this).parent();

<style type="text/css">
body{height:100%;background:#fff;color:#1f1f1f;font-family:Arial,Verdana,sans-serif;font-size:13px;padding:7px 0;}
ul, ol{list-style:none;}
.text-center {text-align: center; padding: 10px 0;}
.wrap {width: 960px; margin: 0 auto;}

/* Tutorial CSS */
/*Form styles*/
.styled {
font: 15px Arial, sans-serif; 
width: 422px; 
margin: 40px auto; 
background: url(images/bg_form.png) no-repeat 0 0; 
padding-top: 20px;
.styled fieldset {
background: url(images/bg_form.png) no-repeat 0 100%; 
padding: 0 25px 20px 25px; 
position: relative;
.styled fieldset h3 { 
font: 24px bold Arial, sans-serif; 
color: #555;
margin-bottom: 0.5em;
/* Form rows */
.styled fieldset li.form-row {
margin-bottom: 5px; 
padding: 3px 0; 
clear: both; 
position: relative;
.styled label {
display: block; 
font-weight: bold; 
float: left; 
width: 100px; 
line-height: 24px; 
padding-top: 4px; 
color: #555;
.styled label.double {
padding-top: 0; 
line-height: 20px; 
margin-top: -3px;
.styled fieldset li.button-row {
margin-bottom: 0; 
padding: 5px 0 0; 
text-align: right;
/* Text input styles */
/* Default */
.styled input.text-input {
height: 22px;
width: 254px;
padding: 5px 8px; 
background: url(images/bg_input.png) no-repeat 0 0;  
border: none;   
font: normal 15px Arial, sans-serif;
color: #333;
line-height: 1em;

/* Form Validation */
.styled span.error {
font: bold 11px Arial, sans-serif;
text-shadow: 1px 1px 1px #000;
display: none; 
background: url(images/arrow_error.png) no-repeat 0 center; 
height: 11px;
padding: 7px 15px 10px 20px; 
line-height: 1em; 
position: absolute; 
top: 3px; 
right: 0; 
border-right: 1px solid #6c0202;
.styled fieldset li.error input.text-input {
background-position: 0 -64px;
<div class="wrap">
  <h2 class="text-center">jQuery Validation With Animated Error Messages</h2>
          <form id="form-sign-up" class="styled" action="" method="post">
			  <h3>Register Now!</h3>
			    <li class="form-row"><label>Name:</label>
				  <input name="name" type="text" class="text-input required" />
				<li class="form-row"><label>Email:</label>
				  <input name="email" type="text" id="register-email" class="text-input required email" />
				<li class="form-row"><label>Password:</label>
				  <input name="password" type="password" id="password-1" class="text-input required password" />
				<li class="form-row"><label>Repeat Password:</label>
				  <input name="password" type="password" id="password-2" class="text-input required password" />
				<li class="button-row">
				  <input type="image" src="images/btn_sign_up_off.png" alt="Sign Up" value="OK" class="btn-submit img-swap" />
		  <p class="text-center"><a href="http://www.designchemical.com/blog/index.php/jquery/jquery-animated-error-messages-form-validation/">View blog post</a></p>

Can someone help me, please. I need it ASAP.

Thank you PF2G

Recommended Answers

All 3 Replies

You're going to have to doctor that query script a lot to make it fit your form - the two don't have that much in common. Do you know query? If not it may not be worth your time. Depending on the validation you need to do there are less complex ways of checking the input with javascript.

I never worked with javascript, that's why i'm asking for help. At least some hints or something.


You can code up some simple validation by using javascript's document.getElementById function. Call a method in the on click of your submit button or the onSubmit method of the form and then use the getEelementById to locate each control you want to validate. Do you want to simply check the text boxes have something entered in them or check for bad data at the same time?

A really simple function to check if the user name is filled in would look like this:

function validate{
  var userControl = document.getElementById(txtUser);
  if(userControl.value.length < 1) {
    // handle the error
   return false;

Your form will have this included: onSubmit="return validate();" you can then return true to post the form or false to cancel i.e. stay on the same page and handle the errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.