Övning+7+-uppgift+2

Övning 7 Uppgift 2 Ramverk: jQuery SKrivet av: Johan T Uppgift: validera indata i en kontakt formulär.

Källkodslösningen är:

$(document).ready(function {

$('.error').hide; $('.error2').hide;

$('.submit').click(function(event){

var nonError = true;

var data = $('#field_firstname').val; var len = data.length;

// Validating firstname if(len < 1) { $('#lbl_lastname').next.show; nonError = false; } else { $('#lbl_lastname').next.hide; }

var data2 = $('#field_lastname').val; var len = data2.length;

// validating lastname if(len < 1) { $('.error2').show; nonError = false; } else { $('.error2').hide; }

data = $('#field_organisation').val; len = data.length;

// validating organisation if(len < 1) { $('#field_organisation').next.show; nonError = false; } else { $('#field_organisation').next.hide; }

//valid_email(email)

var email= $('#field_email').val; var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

//if( pattern.test(email) )

// validating e-mail if( valid_email(email) ) { //$('#field_email'').next.show; $('#field_email').next.hide; } else { $('#field_email').next.show; nonError = false; }

var str = $("input[name=presentation_type]:checked").val;

// validating discussion and presentation if(str != "discussion") { $('#field_presentation_title').next.hide;

data = $('#field_presentation_title').val;

if(data != "") { $('#field_presentation_title').next.hide; } else { $('#field_presentation_title').next.show; nonError = false; }

} else { $('#field_presentation_title').next.hide; }

var dataaa = $('#field_message').val; var lenn = dataaa.length;

// Validating message if(lenn <= 255) { $('#field_message').next.hide; } else { $('#field_message').next.show; nonError = false; }

// validating if need to correct any error if(!nonError) { event.preventDefault; }

}); // saveForm

}); // ready

function valid_email(email) { var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/; return pattern.test(email); }