Saturday, February 23, 2013

how to trim a string using javascript

Many modern browsers (such as Firefox 3, Opera 11, Safari 5.x, Chrome 10, Internet Explorer 9 in standards mode) support the string.trim() method, which allows you to rewrite the above much shorter:

str = str.trim()

For browsers that do not support string.trim(), e.g. Internet Explorer 8 or earlier, you can add your own trim() method to string object's prototype by including the following script in the <HEAD> section of your page, prior to any script that uses string.trim():

if (!String.prototype.trim) {
 String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,'');
 }
}

   

Friday, February 22, 2013

add forign key in mysql alter table

CREATE TABLE IF NOT EXISTS `parent` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `child` (
  `id` int(11) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  KEY `par_ind` (`parent_id`),
  KEY `child_to_module_fk_company` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `child`
  ADD CONSTRAINT `company_to_module_fk_company` 
    FOREIGN KEY (`parent_id`)
    REFERENCES `parent` (`id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;