Home > OS >  How to redirect to external url with Javascript after function return true
How to redirect to external url with Javascript after function return true

Time:01-19

I am ab beginner with javascript and I have checked a lot of post regarding that topic but till now I haven't find a answer for that. So I would be glad if anybody could help me with this task:

I have a form with an input tag for a partner number which the user shall enter. After click on the submit button the function checkNumber is activated. When the entered partner number is not true then an alert shall be shown. When the partner number is no true then the user is forwarded to an external URL from us. It should be the first step of an Login Check.

So far so good. I have written the form and the function to check the partner number (see below-mentioned code). But How can I redirect the user to the external URL after the partner number check return true? I have tried with window.location.replace and window.location.href but then the complete

I would be really happy if anybody could help me.

`<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>login</title>

<!-- Validation of entered value -->
<script>
function checkNumber(form)
{
var partnernr = form.partnernr.value;
var regex1 = /^[0-9]{6}?$/;
var regex2 = /^[0-9]{5,6}(\ |-|\/)[0-9]{1}?$/;
if(!regex1.test(partnernr))
{
if (!regex2.test(partnernr))
{
alert('Your partner number is not valid.');
form.partnernr.focus();
return false;
}
}
return true;
}
window.location.replace = 'https://www.xyz.de'
}
</script>

</head>

<body>

<form action="" method="post" onsubmit="return checkNumber(this)">

<input type="text" name="partnernr" placeholder="e.g. 600000 / 600000-1"" />

<input type="submit" name="submit"  value="Start POSTIDENT-Method"  />

</form>

</body>
</html>`

CodePudding user response:

the code window.location.href = "https://google.com" should do the job.

CodePudding user response:

Looks like you want to prevent the default submit behavior and then redirect to a new page if your check function is successful.

You can add a new script tag to the bottom of your body (so the DOM gets parsed and you can grab the form element), then prevent the default submit behavior and redirect if your check passes.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>login</title>

    <!-- Validation of entered value -->
    <script>
        function checkNumber(form) {
            var partnernr = form.partnernr.value;
            var regex1 = /^[0-9]{6}?$/;
            var regex2 = /^[0-9]{5,6}(\ |-|\/)[0-9]{1}?$/;
            if (!regex1.test(partnernr)) {
                if (!regex2.test(partnernr)) {
                    alert('Your partner number is not valid.');
                    form.partnernr.focus();
                    return false;
                }
            }
            return true;
        }
    </script>
</head>

<body>
    <form id="my-form" action="" method="post">
        <input type="text" name="partnernr" placeholder="e.g. 600000 / 600000-1" />
        <input type=" submit" name="submit"  value="Start POSTIDENT-Method" />
    </form>

    <script>
        document.getElementById('my-form').addEventListener('submit', e => {
            e.preventDefault();
            if (checkNumber(e.target)) {
                window.location.href = 'https://www.xyz.de'
            }
        });
    </script>
</body>
</html>
  • Related