0

I'm trying to find phone number and mobile number from an array with jquery.

jQuery:

var data = $('#PhoneLabel').text();
var array = data.split(', ');
$.grep(array, function (item, index) {
    if (item.charAt(0) === '0' && item.charAt(1) === '9') {
        var mob = item;
        $('#UserMobMenuSMS').attr('href', 'sms:' + mob);
        $('#UserMobMenuTel').attr('href', 'tel:' + mob);
        alert('its mobile');
    } else if (item.charAt(0) !== '0' && item.charAt(1) !== '9') {
        $('#UserMobMenuSMS').addClass('disableUserMenuMob');
        $('#UserMobMenuSMS').attr('href', '#');
        alert('there is no mobile');
    } else {
        var phone = item;
        $('#UserMobMenuTel').attr('href', 'tel:' + phone);
        alert('its phone');
    }
});

if number start with 09 it is mobile number and else it's phone number. and there is 3 forms in html:

first, only phone number:

Not Working

<span id="PhoneLabel" class="GlobalTelColor">021-88915907 , 021-88915907</span>

second, phone number and mobile number:

Working good

<span id="PhoneLabel" class="GlobalTelColor">09127007008 , 021-55293301-6 , 021-55293003</span>

third, only mobile number:

Working good

<span id="PhoneLabel" class="GlobalTelColor">09195328272</span>

the problem is in first form, if there is no mobile number in array, I want to use phone number in #UserMobMenuTel and disable #UserMobMenuSMS with disableUserMenuMob class. I mean when there is no mobile number UserMobMenuSMS should get disableUserMenuMob class, but it's not working, i guess i used wrong method in this case, is there any better way and solution?.

please check out the JSFiddle Demo.

1 Answer 1

1

Changed it a bit there. added the disableUserMenuMob classes as intial and then removeClass as you find a mobile. If no mobile found, do nothing.

You had 3 if else switches and I felt 2 is sufficient for what you want

https://jsfiddle.net/m6eza3q1/4/

Sign up to request clarification or add additional context in comments.

4 Comments

i tried this before, but it's not working good, please un comment third form. you know, the problem is second if not working at all.
I did that, But what is the result you need there? Should the SMS be enabled since a mobile is available?
Yes, when there is a mobile number SMS should enable.
Editted my answer above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.