DouglasMcKibbin Posted May 1, 2014 Share Posted May 1, 2014 Having trouble returning focus to a data element on an entry firm that does not pass validation criteria in Safari and Firefox. Instead the focus goes to the very next data element on the form. For example …. function CheckSS(strng,x) { if (strng == "") { alert("You didn't enter a Social Security Number."); HighlightRed(x); return; } else { if ((strng.length < 9) || (strng.length > 9)) { alert("The Social Security Number is the wrong length. Just use numbers, no dashes... like 123456789 not " + strng); HighlightRed(x); return; } else { var illegalChars = /W#/; if (illegalChars.test(strng)) { alert( "The Social Security Number contains illegal characters. Just use numbers, no dashes."); HighlightRed(x); return; } } } HighlightWhite(x) } Called from ….. SS#: <INPUT name="personal_SS" id="personal_SS" type="text" size=12 value="#########" onblur="CheckSS(this.value,this.id)" onfocus="DoClear(this,this.id)" tabindex=5> I'm sure I must have done something wrong … any ideas? Thanks Douglas McKibbin Link to comment Share on other sites More sharing options...
justsomeguy Posted May 1, 2014 Share Posted May 1, 2014 So the field blurs, and you want to validate the field and then focus it again if it didn't pass validation? I don't see where you're trying to focus it again. Link to comment Share on other sites More sharing options...
DouglasMcKibbin Posted May 1, 2014 Author Share Posted May 1, 2014 Right. So how do I focus back on the field that didn't pass validation? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 1, 2014 Share Posted May 1, 2014 The fields have a focus method that can call, e.g.:field_element.focus(); Link to comment Share on other sites More sharing options...
DouglasMcKibbin Posted May 1, 2014 Author Share Posted May 1, 2014 Thanks, I tried to no avail ….. function CheckSS(strng,x) { if (strng == "") { alert("You didn't enter a Social Security Number."); HighlightRed(x); document.getElementById("x").focus(); document.getElementById("x").scrollIntoView();return; } else { if ((strng.length < 9) || (strng.length > 9)) { alert("The Social Security Number is the wrong length. Just use numbers, no dashes... like 123456789 not " + strng); HighlightRed(x); document.getElementById("x").focus(); document.getElementById("x").scrollIntoView();return; } else { var illegalChars = /W#/; if (illegalChars.test(strng)) { alert( "The Social Security Number contains illegal characters. Just use numbers, no dashes."); HighlightRed(x); document.getElementById("x").focus(); document.getElementById("x").scrollIntoView();return; } } HighlightWhite(x) } and the same thing happens. I know this must just be semantics, but I'm a bit lost. Thanks so much, Douglas McKibbin Link to comment Share on other sites More sharing options...
DouglasMcKibbin Posted May 1, 2014 Author Share Posted May 1, 2014 Should I just use …. document.getElementById(x).focus(); ?? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 1, 2014 Share Posted May 1, 2014 If x is a variable that contains the ID, then yes, don't put quotes around it. Link to comment Share on other sites More sharing options...
DouglasMcKibbin Posted May 2, 2014 Author Share Posted May 2, 2014 The above works in IE but not in Safari or Firefox. Should I code differently for macs, and if so how? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 2, 2014 Share Posted May 2, 2014 getElementById and focus should work everywhere. Make sure you're checking the browser consoles for Javascript errors. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now