Tuesday, July 17, 2012

Jquery Validations


Validates Numeric Characters Only

Accepts only 0 – 9
$('.keyup-numeric').keyup(function() {
$('span.error-keyup-1').hide();
var inputVal = $(this).val();
var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
if(!numericReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-1">Numeric characters only.</span>');
}
});

No Special Characters

Allows only letters, numbers and spaces. All other characters will return an error
$('.keyup-characters').keyup(function() {
$('span.error-keyup-2').remove();
var inputVal = $(this).val();
var characterReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
if(!characterReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-2">No special characters allowed.</span>');
}
});

Maximum of 8 Characters

Allows all characters up to a maximum of 8. Useful for passwords, etc. The value can easily be increased/descreased by changing the {0,8}
$('.keyup-limit-8').keyup(function() {
$('span.error-keyup-3').remove();
var inputVal = $(this).val();
var characterReg = /^([a-zA-Z0-9]{0,8})$/;
if(!characterReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-3">Maximum 8 characters.</span>');
}
});

US Phone Number

Allows numbers 2-9 for the first and second group of 3 followed by 0-9 for the last 4 with the groups separated by “-” e.g:
  • 234-234-1234 = OK
$('.keyup-phone').keyup(function() {
$('span.error-keyup-4').remove();
var inputVal = $(this).val();
var characterReg = /^[2-9]\d{2}-\d{3}-\d{4}$/;
if(!characterReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-4">Format xxx-xxx-xxxx</span>');
}
});

Validate Date Format

Allows date format – mm/dd/yyyy – including “/”. All other combinations will return errors e.g:
  • 01/31/2001 = OK
$('.keyup-date').keyup(function() {
$('span.error-keyup-5').remove();
var inputVal = $(this).val();
var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/;
if(!dateReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-5">Invalid date format.</span>');
}
});

Check Email Address Format

This is a standard regular expression, which is used to validate email addresses to ensure they follow the standard format:
  • email@email.com = OK
$('.keyup-email').keyup(function() {
$('span.error-keyup-7').remove();
var inputVal = $(this).val();
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-7">Invalid Email Format.</span>');
}
});
Email Validation  
//Code
<form method="post" name="form1" action="">
<fieldset>
<label>Email Address:</labe>
<input type="text" name="UserEmail" id="UserEmail" value="" size="32" />
<input type="submit" value="Submit" id="btn-submit" />
</fieldset>
</form>
//Jquery Script for Email Validation
$(document).ready(function() {
$('#btn-submit').click(function() {
$(".error").hide();
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#UserEmail").val();
if(emailaddressVal == '') {
$("#UserEmail").after('<span class="error">Please enter your email address.</span>');
hasError = true;
}
else if(!emailReg.test(emailaddressVal)) {
$("#UserEmail").after('<span class="error">Enter a valid email address.</span>');
hasError = true;
}
if(hasError == true) { return false; }
});
});
How to copy newline characters from HTML div to textarea in jQuery
<div id='myDiv'>
    Line 1.<br />
    Line 2<br />
    These are &ltspecial&gt; characters &amp; must be escaped !@@&gt;&lt;&gt;</div>
<input type='button' value='click' id='myButton' />
<textarea id='myTextArea'></textarea>
<script>
    $(document).ready(function () {
        $('#myButton').click(function () {
            var text = $('#myDiv').text();
            $('#myTextArea').val(text);
        });
    });
</script>
The aboce Script works in Firefox and Chrome and where asint IE7 its breaking so the code modifies like this,
$('#myButton').click(function () {
            var text = $('#myDiv').html().replace(/\<br\>/gi,"\r\n");
            $('#myTextArea').val(text);
        });
 

No comments:

Post a Comment