Need help for onBlur event












0















I have a page contained multiple input (creating from code behind) in which two of them need to check whether or not is empty.



So I simply create 2 input as below



<input id='input_1' title='first_input' tabindex='1' onBlur='check_empty(document.form1.input_1);'></input>
<input id='input_2' title='second_input' tabindex='2' onBlur='check_empty(document.form1.input_2);'></input>


and my JavaScript:



function check_empty(field){
if(field.value === ""){
alert('Please enter' + field.title);
field.focus();
}
};


The problem I've been facing is whenever I leave input1 empty and press TAB , the focus is now on input2 and from the JavaScript it change the focus from input2 to it and then since the input2 lost focus the JavaScript change the focus from input1 to it too! and so on.



What should I do?










share|improve this question

























  • the onBlue events are supposed to call the check_empty function, but where is it declared because I am seeing the myFnc instead ???

    – marvinIsSacul
    Nov 20 '18 at 10:54






  • 1





    Did my answer solve your problem?

    – Ahmad
    Nov 20 '18 at 12:36











  • @marvinIsSacul Sorry about that , I've change it now

    – Izle
    Nov 21 '18 at 2:14
















0















I have a page contained multiple input (creating from code behind) in which two of them need to check whether or not is empty.



So I simply create 2 input as below



<input id='input_1' title='first_input' tabindex='1' onBlur='check_empty(document.form1.input_1);'></input>
<input id='input_2' title='second_input' tabindex='2' onBlur='check_empty(document.form1.input_2);'></input>


and my JavaScript:



function check_empty(field){
if(field.value === ""){
alert('Please enter' + field.title);
field.focus();
}
};


The problem I've been facing is whenever I leave input1 empty and press TAB , the focus is now on input2 and from the JavaScript it change the focus from input2 to it and then since the input2 lost focus the JavaScript change the focus from input1 to it too! and so on.



What should I do?










share|improve this question

























  • the onBlue events are supposed to call the check_empty function, but where is it declared because I am seeing the myFnc instead ???

    – marvinIsSacul
    Nov 20 '18 at 10:54






  • 1





    Did my answer solve your problem?

    – Ahmad
    Nov 20 '18 at 12:36











  • @marvinIsSacul Sorry about that , I've change it now

    – Izle
    Nov 21 '18 at 2:14














0












0








0








I have a page contained multiple input (creating from code behind) in which two of them need to check whether or not is empty.



So I simply create 2 input as below



<input id='input_1' title='first_input' tabindex='1' onBlur='check_empty(document.form1.input_1);'></input>
<input id='input_2' title='second_input' tabindex='2' onBlur='check_empty(document.form1.input_2);'></input>


and my JavaScript:



function check_empty(field){
if(field.value === ""){
alert('Please enter' + field.title);
field.focus();
}
};


The problem I've been facing is whenever I leave input1 empty and press TAB , the focus is now on input2 and from the JavaScript it change the focus from input2 to it and then since the input2 lost focus the JavaScript change the focus from input1 to it too! and so on.



What should I do?










share|improve this question
















I have a page contained multiple input (creating from code behind) in which two of them need to check whether or not is empty.



So I simply create 2 input as below



<input id='input_1' title='first_input' tabindex='1' onBlur='check_empty(document.form1.input_1);'></input>
<input id='input_2' title='second_input' tabindex='2' onBlur='check_empty(document.form1.input_2);'></input>


and my JavaScript:



function check_empty(field){
if(field.value === ""){
alert('Please enter' + field.title);
field.focus();
}
};


The problem I've been facing is whenever I leave input1 empty and press TAB , the focus is now on input2 and from the JavaScript it change the focus from input2 to it and then since the input2 lost focus the JavaScript change the focus from input1 to it too! and so on.



What should I do?







javascript asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 2:13







Izle

















asked Nov 20 '18 at 10:36









IzleIzle

304




304













  • the onBlue events are supposed to call the check_empty function, but where is it declared because I am seeing the myFnc instead ???

    – marvinIsSacul
    Nov 20 '18 at 10:54






  • 1





    Did my answer solve your problem?

    – Ahmad
    Nov 20 '18 at 12:36











  • @marvinIsSacul Sorry about that , I've change it now

    – Izle
    Nov 21 '18 at 2:14



















  • the onBlue events are supposed to call the check_empty function, but where is it declared because I am seeing the myFnc instead ???

    – marvinIsSacul
    Nov 20 '18 at 10:54






  • 1





    Did my answer solve your problem?

    – Ahmad
    Nov 20 '18 at 12:36











  • @marvinIsSacul Sorry about that , I've change it now

    – Izle
    Nov 21 '18 at 2:14

















the onBlue events are supposed to call the check_empty function, but where is it declared because I am seeing the myFnc instead ???

– marvinIsSacul
Nov 20 '18 at 10:54





the onBlue events are supposed to call the check_empty function, but where is it declared because I am seeing the myFnc instead ???

– marvinIsSacul
Nov 20 '18 at 10:54




1




1





Did my answer solve your problem?

– Ahmad
Nov 20 '18 at 12:36





Did my answer solve your problem?

– Ahmad
Nov 20 '18 at 12:36













@marvinIsSacul Sorry about that , I've change it now

– Izle
Nov 21 '18 at 2:14





@marvinIsSacul Sorry about that , I've change it now

– Izle
Nov 21 '18 at 2:14












1 Answer
1






active

oldest

votes


















0














1- Your onblur is pointing to a non-existing function check_empty().



2- You need to invoke the function with proper parameters so that it can check the field. There is no need to pass ids to the function. You can just pass this and it will point to the element that invoked the function.



3- Having the alert() pop up will cause the focus automatically be removed from the text-box whenever you click the OK button to dismiss the alert, causing the onblur function to get triggered infinity. A better solution is to post the message on the page in a way to inform the user without disrupting his/her experience.






function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>








share|improve this answer
























  • Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

    – Izle
    Nov 21 '18 at 2:18













  • On your code snippet , I clicked the first box then press TAB it shows up the same problem.

    – Izle
    Nov 21 '18 at 2:20











  • @Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

    – Ahmad
    Nov 21 '18 at 3:37











  • I've tried that and still face the same problem. May be need to change the way of input validation.

    – Izle
    Nov 21 '18 at 4:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391109%2fneed-help-for-onblur-event%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














1- Your onblur is pointing to a non-existing function check_empty().



2- You need to invoke the function with proper parameters so that it can check the field. There is no need to pass ids to the function. You can just pass this and it will point to the element that invoked the function.



3- Having the alert() pop up will cause the focus automatically be removed from the text-box whenever you click the OK button to dismiss the alert, causing the onblur function to get triggered infinity. A better solution is to post the message on the page in a way to inform the user without disrupting his/her experience.






function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>








share|improve this answer
























  • Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

    – Izle
    Nov 21 '18 at 2:18













  • On your code snippet , I clicked the first box then press TAB it shows up the same problem.

    – Izle
    Nov 21 '18 at 2:20











  • @Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

    – Ahmad
    Nov 21 '18 at 3:37











  • I've tried that and still face the same problem. May be need to change the way of input validation.

    – Izle
    Nov 21 '18 at 4:23
















0














1- Your onblur is pointing to a non-existing function check_empty().



2- You need to invoke the function with proper parameters so that it can check the field. There is no need to pass ids to the function. You can just pass this and it will point to the element that invoked the function.



3- Having the alert() pop up will cause the focus automatically be removed from the text-box whenever you click the OK button to dismiss the alert, causing the onblur function to get triggered infinity. A better solution is to post the message on the page in a way to inform the user without disrupting his/her experience.






function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>








share|improve this answer
























  • Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

    – Izle
    Nov 21 '18 at 2:18













  • On your code snippet , I clicked the first box then press TAB it shows up the same problem.

    – Izle
    Nov 21 '18 at 2:20











  • @Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

    – Ahmad
    Nov 21 '18 at 3:37











  • I've tried that and still face the same problem. May be need to change the way of input validation.

    – Izle
    Nov 21 '18 at 4:23














0












0








0







1- Your onblur is pointing to a non-existing function check_empty().



2- You need to invoke the function with proper parameters so that it can check the field. There is no need to pass ids to the function. You can just pass this and it will point to the element that invoked the function.



3- Having the alert() pop up will cause the focus automatically be removed from the text-box whenever you click the OK button to dismiss the alert, causing the onblur function to get triggered infinity. A better solution is to post the message on the page in a way to inform the user without disrupting his/her experience.






function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>








share|improve this answer













1- Your onblur is pointing to a non-existing function check_empty().



2- You need to invoke the function with proper parameters so that it can check the field. There is no need to pass ids to the function. You can just pass this and it will point to the element that invoked the function.



3- Having the alert() pop up will cause the focus automatically be removed from the text-box whenever you click the OK button to dismiss the alert, causing the onblur function to get triggered infinity. A better solution is to post the message on the page in a way to inform the user without disrupting his/her experience.






function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>








function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>





function check_empty(field){
var msg = document.getElementById('message');
msg.innerHTML = '';

if(field.value === ""){
field.focus();
msg.innerHTML = '<br>Please enter ' + field.title;

}
};

Click inside a field, then try to leave it empty. <br>
<input id='input_1' title='first_input' tabindex='1' onBlur="check_empty(this);">
<input id='input_2' title='second_input' tabindex='2' onBlur="check_empty(this);">
<div id='message'></div>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 10:59









AhmadAhmad

8,20543463




8,20543463













  • Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

    – Izle
    Nov 21 '18 at 2:18













  • On your code snippet , I clicked the first box then press TAB it shows up the same problem.

    – Izle
    Nov 21 '18 at 2:20











  • @Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

    – Ahmad
    Nov 21 '18 at 3:37











  • I've tried that and still face the same problem. May be need to change the way of input validation.

    – Izle
    Nov 21 '18 at 4:23



















  • Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

    – Izle
    Nov 21 '18 at 2:18













  • On your code snippet , I clicked the first box then press TAB it shows up the same problem.

    – Izle
    Nov 21 '18 at 2:20











  • @Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

    – Ahmad
    Nov 21 '18 at 3:37











  • I've tried that and still face the same problem. May be need to change the way of input validation.

    – Izle
    Nov 21 '18 at 4:23

















Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

– Izle
Nov 21 '18 at 2:18







Thank you for your answer, but it's not working. I also run your code snippet and it's not working either.

– Izle
Nov 21 '18 at 2:18















On your code snippet , I clicked the first box then press TAB it shows up the same problem.

– Izle
Nov 21 '18 at 2:20





On your code snippet , I clicked the first box then press TAB it shows up the same problem.

– Izle
Nov 21 '18 at 2:20













@Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

– Ahmad
Nov 21 '18 at 3:37





@Izle that is because of Stackoverflow snippet design. If you test this code in your website, it will work. Try adding more text boxes with same onblur event. Only last textbox will not trigger it if you press tab. But if you try to focus on other textboxes it will be triggered even for the last field

– Ahmad
Nov 21 '18 at 3:37













I've tried that and still face the same problem. May be need to change the way of input validation.

– Izle
Nov 21 '18 at 4:23





I've tried that and still face the same problem. May be need to change the way of input validation.

– Izle
Nov 21 '18 at 4:23


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391109%2fneed-help-for-onblur-event%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith