Valerii Vasin (valerii) wrote in changelog,
Valerii Vasin
valerii
changelog

[ljcom] r12411: LJSUP-12885: SMS notification - Add Taji...

Committer: vvasin
LJSUP-12885: SMS notification - Add Tajikistan. Changed algorithm of phone number verification.
U   trunk/htdocs/js/editprofile.js
Modified: trunk/htdocs/js/editprofile.js
===================================================================
--- trunk/htdocs/js/editprofile.js	2012-08-03 10:57:25 UTC (rev 12410)
+++ trunk/htdocs/js/editprofile.js	2012-08-03 11:18:41 UTC (rev 12411)
@@ -1,17 +1,5 @@
 jQuery(function($)
 {
-	var options = {
-		defaultCode: '+7'
-	};
-
-	function checkDEF(val)
-	{
-		//var def = parseInt(val.substr(0,3),10);
-		var def = parseInt( val.replace( /^(\+7|7|8)/, '', val ).substr(0,3),10);
-
-		return ($.inArray(def, supportedDEF) > -1);
-	}
-
 	/**
 	 * PhoneNumber looks into the two inputs and checks their values according to the mask.
 	 */
@@ -21,56 +9,59 @@
 		 * @param Node number
 		 */
 		init: function(code, number, onUpdate) {
-			var self = this;
+			var that = this;
 			this._onUpdate = onUpdate || jQuery.noop;
 
 			this._code = code.attr('autocomplete', 'off').input(function() {
-					self.updateState();
-				});
+				that.updateState();
+			});
+
 			this._number = number.attr('autocomplete', 'off').input(function() {
-					self.updateState();
-				});
+				that.updateState();
+			});
+
 			this.updateState();
 		},
 
 		updateState: function() {
-			/*
-			 var mask = /^\d{10}$/,
-			 stripChars = "()\\- ";
+			var stripRegexp = /[()\-\s\\\/]/g,
+				// trimmed value of input - needed to detect initial state
+				value = this._number.val().trim(),
+				// phone number, contains only numbers and includes country,
+				// city/operator codes and actually number
+				phone = value.replace(stripRegexp, '')
+							.replace(/^\+/, '')
+							// replace 8 as internal Russia code
+							.replace(/^8/, '7'),
+				parsed = parseNumber(phone),
+				state = value.length === 0 ? 'initial' :
+						!parsed.def ? 'unsupporteddef' :
+						!parsed.phone ? 'badnum' :
+						'correctnum';
 
-			 var code = this._code.val(),
-			 number = $.trim( this._number.val() ).replace(new RegExp("[" + stripChars + "]", "g"),"");
+			this._onUpdate(state);
+		},
 
-			 var state = 'correctnum';
+		/**
+		 * Parse number from input and checks def and phone
+		 * @param  {String} number Cleaned value (from extra characters)
+		 * @return {Object}        Hash with {def, phone} contains correct def and phone
+		 * or null, if they are incorrect
+		 */
+		parseNumber: function (number) {
+			var regexp = new RegExp('^('+ supportedDEF.join('|') +')'),
+				match = number.match(regexp),
+				phone,
+				result = { def: null, phone: null };
 
-			 if( number.length == 0 && code === options.defaultCode )
-			 {
-			 state = 'initial';
-			 }
-			 else if( !( mask.test( number ) ) || !( /^(\+7|7|8)$/.test( code ) ) )
-			 {
-			 state = 'badnum';
-			 }
-			 else if( !checkDEF( number ) )
-			 {
-			 state = 'unsupporteddef';
-			 }
-			 */
-			var mask = /^(\+7|7|8)\d{10}$/, stripChars = "()\\- ";
-
-			var num = $.trim(this._number.val()).replace(new RegExp("[" + stripChars + "]", "g"), "");
-
-			var state = 'correctnum';
-
-			if (num.length == 0) {
-				state = 'initial';
-			} else if (!( mask.test(num) )) {
-				state = 'badnum';
-			} else if (!checkDEF(num)) {
-				state = 'unsupporteddef';
+			if (match) {
+				def = match[0];
+				phone = number.slice(def.length);
+				result.def = def;
+				result.phone = /^\d{7}$/.test(phone) ? phone : null;
 			}
 
-			this._onUpdate(state);
+			return result;
 		}
 	};
 
@@ -143,7 +134,7 @@
 			this.ui.sendCodeBtn.click(function() {
 				var code = that.ui.verCode.val();
 
-				if (code.length == 0)
+				if (code.length === 0)
 					return;
 
 				$.get(that.endpoint + '?action=verify_short_code&shortcode=' + code, function(result) {

Tags: invis89, js, ljcom, vvasin
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments