/    Sign up×
Community /Pin to ProfileBookmark

Focus on a field

I need help with a JS function.
In a PHP form I have a field and, when the focus is lost (moving to another field), it is immediately validated by an onBlur event which in case of error opens a sweet alert.
So far everything Ok.
I would like, however, that when the alert is closed (click OK) the focus returns to the invalid field.
To do this I was suggested to create a small javascript function; as I’m not js expert, can someone help me with an example?
Thanks and best regards.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSAMay 01.2020 — Can you please share the screenshot of the coding part? it will more helpful to us to understand exact problem.
Copy linkTweet thisAlerts:
@NasarhayatMay 01.2020 — Yes please share the screenshot.
Copy linkTweet thisAlerts:
@SempervivumMay 01.2020 — please share the screenshot of the coding part[/quote]
Please don't. A screenshot is not searchable and copy&paste is not possible when one intends to make a test site. Please copy the text and post it inside code tags: `your code here`

I searched for an onclose event for sweetalert but didn't find any. Instead the same can be done by promises as explained here:

https://github.com/t4t5/sweetalert

Then you can set the focus by use of the function focus() to the input field again.
Copy linkTweet thisAlerts:
@IwawaauthorMay 02.2020 — @Sempervivum#1618037

Ok, this is the code used in the event onBlur.
$login={fld_login};
$sql_command =
"select fld_login from tb_users where fld_login = '{fld_login}'";
sc_lookup(ds_user, $sql_command);
if ({ds_user} != false)
{
$params = array(
'title' => "Error",
'type' => "error",
'width' => '350px',
'confirmButtonText' => 'OK');
$desc="The entered login: ".$login." "."already used!";
sc_alert($desc, $params);
// focus function????
} else {
$params = array(
'title' => "Error",
'type' => "error",
'width' => '350px',
'confirmButtonText' => 'OK');
$desc="This field cannot be empty!";
sc_alert($desc, $params);
// focus function ???
}

When I leave the field, this code validates the field and works properly. If an error occurs the sc_alert function opens a popup for the warning.

When the popup is closed I would return the focus on the same invalid field.
Copy linkTweet thisAlerts:
@SempervivumMay 02.2020 — Sorry, when reading "sweet alert" I thought of this:

https://sweetalert.js.org

However you are using script case. Unfortunately I don't have any knowledge about this. Maybe someone else can assist you.
×

Success!

Help @Iwawa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...