Tuesday, September 4, 2012

Trim in Javaascript

Trim string in Javascript 
One simple way of trimming a string is to make use of a powerful regular
expression—as opposed to iterating and removing the space characters 
one by one. To see how this works, save the code below as an .htm file, and 
then load the file into IE. Click in the text field and then tab out of it to
see the code in action.  
<script type="text/javascript">
    function TrimString()
    {
        var txtObj = document.getElementById("txtTrim");
        txtObj.value = txtObj.value.replace(/^\s+/,""); //Left trim        
        txtObj.value = txtObj.value.replace(/\s+$/,""); //Right trim
       //<input type="text" id="txtTrim" onblur="TrimString(this.value);" />
 
 }
   </script>


 

Regular Expration via Javascript
 
<script type="text/javascript">
var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]
{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 
var ck_username = /^[A-Za-z0-9_]{1,20}$/;
var ck_password =  /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;

function validate(form){
var name = form.name.value;
var email = form.email.value;
var username = form.username.value;
var password = form.password.value;
var gender = form.gender.value;
var errors = [];
 
 if (!ck_name.test(name)) {
  errors[errors.length] = "You valid Name .";
 }
 if (!ck_email.test(email)) {
  errors[errors.length] = "You must enter a valid email 
address.";
 }
 if (!ck_username.test(username)) {
  errors[errors.length] = "You valid UserName no special 
char .";
 }
 if (!ck_password.test(password)) {
  errors[errors.length] = "You must enter a valid Password ";
 }
 if (gender==0) {
  errors[errors.length] = "Select Gender";
 }
 if (errors.length > 0) {

  reportErrors(errors);
  return false;
 }
  return true;
}
function reportErrors(errors){
 var msg = "Please Enter Valide Data...\n";
 for (var i = 0; i<errors.length; i++) {
 var numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
}
 alert(msg);
}
</script>
 

jQuery – Reverse Each

There may be some situations where you need to step through dom elements in reverse order, in this case the following code should help you:
 
jQuery(jQuery('div.child').get().reverse()).each(function(i) {
    //do stuff
});
 
<div class="parent">
    <div class="child">A</div>
    <div class="child">B</div>
    <div class="child">C</div>
</div>
 
 

No comments:

Post a Comment