根据所选选项重定向到3页并进行验证 [英] Redirect to 3 pages depending of selected option with validation

查看:94
本文介绍了根据所选选项重定向到3页并进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重定向到基于所选选项的页面,但是使用javascript验证。

I'm trying to redirect to a page based on the selected option, but with javascript validation.

我有这样的表单:

<form action="verify.php" method="post">

<label for="class_year" class="inner_text">Year Range</label>
<select id="class_year" name="year">
    <option value="a">50</option>
    <option value="b">60</option>
    <option value="c">70</option>
    <option value="c">80</option>
    <option value="c">90</option>
</select>

<label for="dob" class="inner_text">Date of Birth (YY-MM-DD)</label>
<input name="dob" type="text" class="false" id="dob" maxlength="10">

</form>

现在我有这个verify.php文件,如果你选择1950,它会重定向到a.html:

Now I have this verify.php file that redirect for example to a.html if you choose 1950:

<?php

header('Location: '.$_POST['year'].'.php');

?>

我想要做的是验证访问者选择的内容。例如,如果您在文本框中写入51-02-28,则表示您必须选择1950,因此您将被重定向到a.html;如果你写的是例如61-02-28,但是你选择了1950,那么你就无法提交表格了。如果您在41-02-29之间编写任何其他内容,那么您也会收到提醒,并且不会提交表单。前2个选项重定向到a.html和b.html,最后3个选项重定向到同一页面c.html。

What I want to do is to verify what my visitors choose. For example if you write 51-02-28 in text box, that mean you must choose 1950, so you will be redirected to a.html; if you write for example 61-02-28 but you choosed 1950 then you can't submit the form an a alert will show. And if you write anything else for example 41-02-29, then you will be alerted too and the form will not be submited. The first 2 option redirect to a.html and b.html, the last 3 options redirect to the same page c.html .

总之,我只需要验证文本框中的第一个数字。我不知道如何用javascript做到这一点!

In conclusion I need to validate only the first number from the text box. I don't know how to do that with javascript!

我apreciate任何和所有评论,谢谢。

I apreciate any and all comments, thank you.

推荐答案

您可以使用普通的javascript进行验证,重定向时可以使用类似这样的内容而不是PHP重定向

You can go ahead with validation by using normal javascript and while redirecting you can use something like this instead of PHP redirect

你的代码应该是这样的:

Your code should be something like this:

    <form action="verify.php" method="post" id="mainForm">

    <label for="class_year" class="inner_text">Year Range</label>
    <select id="class_year" name="year">
        <option value="50">50</option>
        <option value="60">60</option>
        <option value="70">70</option>
        <option value="80">80</option>
        <option value="90">90</option>
    </select>

    <label for="dob" class="inner_text">Date of Birth (YY-MM-DD)</label>
    <input name="dob" type="text" class="false" id="dob" maxlength="10">

    </form>
<script type="text/javascript">
$("#mainForm").submit(function(){
    var yearVal = $("#dob").val().split("-")[0];
    var redirectObj= [
        ['50','a'],
        ['60','b'],
        ['70','c'],
        ['80','c'],
        ['90','c']
    ];
    if(parseInt(yearVal)>parseInt($("#class_year").val()) && parseInt(yearVal)<(parseInt($("#class_year").val())+10))
    {
    var redirectPage = "";
    redirectObj.forEach(function(entry) {
       if(parseInt(entry[0])==(Math.round(parseInt(yearVal)/10))*10)
    {
    redirectPage = entry[1];
    }
    });
        window.location = redirectPage+".php";
}
else
{
alert("you have entered invalid value");
return false;
}
});

如果你想将它重定向到文本框的第一个字母,你可以使用这样的东西

In case you want to redirect it to the first letter of the textbox you can use something like this

window.location = $("#dob").val().charAt(0)+".php";

您可以在 http://jsfiddle.net/amanchhabra1990/Uy78a/

如果这有助于您,请告诉我。

Please let me know if this helps you.

这篇关于根据所选选项重定向到3页并进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆