Javascript validation for input fields, email, phone number and alphanumeric

// check to see if input is whitespace only or empty function isEmpty(str) { return (!str || 0 === str.length); } function isBlank(str) { return (!str || /^\s*jQuery/.test(str)); } // check to see if input is a valid email address function isEmail(str) { var regexEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{1,4}$/; return (regexEmail.test(str)); } // check to see if… Continue reading Javascript validation for input fields, email, phone number and alphanumeric

Get thumbnail of YouTube video link using Javascript

function getScreen( url, size ) { if(url === null){ return “”; } size = (size === null) ? “big” : size; var vid; var results; results = url.match(“[\\?&]v=([^&#]*)”); vid = ( results === null ) ? url : results[1]; if(size == “small”){ return “http://img.youtube.com/vi/”+vid+”/2.jpg”; } else { return “http://img.youtube.com/vi/”+vid+”/0.jpg”; } }

Javascript to replace broken images

Basically you want to handle the onError event for the image to reassign the source. This can be done without jQuery: function ImgError(source){     source.src = “/images/noimage.gif”;     source.onerror = “”;     return true; } <img src=”someimage.png” onerror=”ImgError(this);”/>

Add custom javascript files to the wordpress site

Use wp_enqueue_script() in an init hook, or if in the admin only then in an admin_menu or admin_init hook. It might look something like this (which assumes you are calling from your theme’s functions.php file; note how I reference the stylesheet directory): <?php add_action(‘init’, ‘mysite_init’); function mysite_init() { $ss_url = get_bloginfo(‘stylesheet_directory’) wp_enqueue_script(‘mysite-scripts’,”$ss_url/js/mysite-scripts.js”); } That’s the basics. Pre-defined and Multiple Dependent Scripts But let’s say you… Continue reading Add custom javascript files to the wordpress site

Support Tabs in Textareas using Javascript

Normally the tab key moves to the next focusable thing. This inserts a tab character in instead. var textarea = document.getElementsByTagName(‘textarea’)[0]; textarea.onkeydown = function(event) { //support tab on textarea if (event.keyCode == 9) { //tab was pressed var newCaretPosition; newCaretPosition = textarea.getCaretPosition() + ” “.length; textarea.value = textarea.value.substring(0, textarea.getCaretPosition()) + ” ” + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);… Continue reading Support Tabs in Textareas using Javascript