Bhagwat Singh

PHP Developer Portfolio

Jquery

Pass a Value to input Field using Jquery.

Hello,

here is the way to pass a value to a input field..
$('.user_login').val("type your text here"); // here is . is name of that input field.

what if input field is hidden, then u need to use this piece of code.
$("input[type=hidden][name=user_login]").val("type your text here");

Change Page title tag using jQuery

Here is a way to change the title tag of the document when page is loaded and ready.


jQuery(document).ready(function() {
jQuery("title").html("Insert your new title here");
});

Jquery Form Validation

There are many ways for form validation using Jquery. here is the simplest example Using Jquery & Alert.

$(document).ready(function(){
$(‘#contactus’).submit(function() {
var error = 0;
var full_name = $(‘#full_name’).val();
var email_address = $(‘#email_address’).val();
var phone_number = $(‘#phone_number’).val();
var country = $(‘#country’).val();
var description = $(‘#description’).val();
var verification_code = $(‘#verification_code’).val();

var reEmail = /^[A-Za-z0-9][a-zA-Z0-9._-][A-Za-z0-9.]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

if (full_name == ” || full_name == ‘Full Name’) {
error = 1;
alert(‘Full Name cannot be empty’);
} else if (email_address == ” || email_address == ‘Email Address’) {
error = 1;
alert(‘Email cannot be empty’);
}else if(email_address.length > 60){
error = 1;
alert(‘Email cannot exceed 60 characters’);
}else if(!reEmail.test(email_address)){
error = 1;
alert(‘Invalid Email’);
} else if (phone_number == ” || phone_number == ‘Phone Number’) {
error = 1;
alert(‘Phone Number cannot be empty’);
} else if (country == ” || country == ‘Country’ ) {
error = 1;
alert(‘Please select country’);
} else if (description == ” ) {
error = 1;
alert(‘Description cannot be empty’);
} else if (verification_code == ” || verification_code == ‘Verification Code’) {
error = 1;
alert(‘Verification Code cannot be empty’);
}

if(error){
return false;
}else{
return true;
}

});
});

Site Nevigation