2

i have one inputfield with some styleClass. i am using jquery on onBlur event to set focus on that inputfield. it is working fine in IE and Chrome but in mozilla it loses focus. what's the issue?

jquery method

function validatePhoneLength(ele) {
          ele.value = ele.value.replace(/[^0-9]/g, "") ;
          if(ele.value.length < 10 && ele.value.length != 0) {
             alert('Phone number must contain atleast 10 digits');
             $('.phone').focus();
        }
  }

vf code

<apex:inputfield styleClass="phone" onblur="validatePhoneLength(this);" 
value="{!Information[Info.fieldPath]}" />
2
  • 1
    Could you put some example code? – Sergej Utko May 24 '13 at 7:27
  • added javascript and vf code. – Pankaj May 24 '13 at 7:33
3

The problem to setting the focus was that you must wait for the event 'blur' to complete. The solution is to use a timer.

    <script>

    function validatePhoneLength(ele) {
        ele.value = ele.value.replace(/[^0-9]/g, "") ;
        if(ele.value.length < 10 && ele.value.length != 0) {
            alert('Phone number must contain atleast 10 digits');
            setTimeout(function() { document.getElementById(mid).focus(); }, 30);
        }
    }

    </script>

    Input <apex:inputField value="{!account.phone}" 
                           styleClass="phone"
                           onblur="validatePhoneLength(this);" 
                           id="myPhone"/>

    <script>
        var mid = '{!$Component.myPhone}';
    </script>
2
  • Great!!!!!!!!!! – Pankaj May 24 '13 at 9:26
  • this seems really hacky and dependent on the page. What is blur doesn't complete in 30ms? – NSjonas Sep 22 '14 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.