Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

Wednesday, October 26, 2011

How to remove option from selectbox options using Jquery

Here I am explaining how to remove an option from selectbox or combobox  options list using Jquery. Jquery is a powerful javascript library that provides a lot of predefined functions to achieve our needs.

For removing the option, using remove function(remove()).  
Add following script in onload function.

$("#selectboxid option[value='value']").remove();


selectboxid = ID of your selectbox
value = value of the option which you want to remove.


$(document).ready(function()){
   $("#select-id option[value='0']").remove();
});


Here option value 0 is removed while  the form load.

http://api.jquery.com/remove/

Thursday, June 30, 2011

Regular expression for validating US phone number

US phone number format validation using regular expression.

The common US phone number format is 3digits-3digits-4digits
eg: 222-333-3333
      111-222-1234
The regular expression for validating this format is

function usphone($string) {
   if(preg_match('/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/', $string)) {
    return true;
  }
  else{
    return false;
  }
}

Validation using Javascript

function isUSphone(phoneno) {
   var filter = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/;
   if (filter.test(phoneno))
        returntrue;
    else
        return false; 
}

In Jquery

if($("#phone-num").val().match(/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/)) {
  //valid phone number
}
else {
  //not a valid number
}

*phone-num -  id of phone number field(input field)

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Maintained by Web Themes