{"id":236,"date":"2011-04-27T19:20:53","date_gmt":"2011-04-27T13:50:53","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=236"},"modified":"2011-04-27T19:22:38","modified_gmt":"2011-04-27T13:52:38","slug":"us-phone-number-validation-by-adding-hypen-automatically-using-javascript","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/javascript\/us-phone-number-validation-by-adding-hypen-automatically-using-javascript\/","title":{"rendered":"US phone number validation by adding hypen automatically using javascript"},"content":{"rendered":"<p>The following code will validate the US phone number format. It will not check for open and closing brackets. it will validate the following format xxx-xxx-xxxx.<\/p>\n<p>Besides it will not allow any other characters other than numbers and hypen to be typed. if any other character is typed in then it is removed using the regular expression in the script.<\/p>\n<p>This script was created for an instant purpose and i may expand it so that will have full control on the keypress.<\/p>\n<p>I have called a function whichkey to find the keycode which is a cross browser code. Each browser handles event differently so we need to find the char code number using a cross browser function and here that is whichkey.<\/p>\n<pre lang='javascript'>\r\n<script type=\"text\/javascript\">\r\nfunction mask(e,f){\r\n\tvar len = f.value.length;\r\n\tvar key = whichKey(e);\r\n\tif(key>47 && key<58)\r\n\t{\r\n\t\tif( len==3 )f.value=f.value+'-'\r\n\t\telse if(len==7 )f.value=f.value+'-'\r\n\t\telse f.value=f.value;\r\n\t}\r\n\telse{\r\n\t\tf.value = f.value.replace(\/[^0-9-]\/,'')\r\n\t\tf.value = f.value.replace('--','-')\r\n\t}\r\n}\r\n\r\nfunction whichKey(e) {\r\n\tvar code;\r\n\tif (!e) var e = window.event;\r\n\tif (e.keyCode) code = e.keyCode;\r\n\telse if (e.which) code = e.which;\r\n\treturn code\r\n\/\/\treturn String.fromCharCode(code);\r\n}\r\n\r\n<\/script>\r\n<\/pre>\n<pre lang='html4strict'>\r\n<input type=\"text\" name=\"phone\" id=\"phone\" \r\nonkeydown=\"mask(event,this)\" onkeyup=\"mask(event,this)\" \r\nmaxlength=\"12\" \/>\r\n<\/pre>\n<p>....<\/p>\n<p>with the above code you can add your own code to make it more useful to you. I had made a very simple attempt to do a validation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following code will validate the US phone number format. It will not check for open and closing brackets. it will validate the following format xxx-xxx-xxxx. Besides it will not allow any other characters other than numbers and hypen to be typed. if any other character is typed in then it is removed using the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[27],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/236"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":3,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":238,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/236\/revisions\/238"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}