Javascript表单验证和重定向

时间:2011-04-25 04:50:03

标签: javascript jquery validation

当输入短于5位时,验证和重定向的问题就出现了(它应该是5才有效)。如果是5,那么用户应该被重定向。正如我所说,当问题无效(短于5)时会出现问题。如果你能建议我如何解决它,我将感激不尽!先谢谢你们。这是代码:

        <script type="text/javascript">
        function redirect(){
            var zip_c = document.myform.tx1.value;
            var sid = document.myform.sid.value;
            if ((myform.tx1.value.length == 5) || (myform.textfielda.value.length > 20)){
                /* if (zip_c == "")
                {
                   return false;
                }
                 */
                var redirect = "http://www.yourcarinsurancefinders.com/lowest-rates/quotes/ppv_hc01/src/results/index.php?zip="+ zip_c+"&sid="+sid; //Edint redirection link here, but dont change '?zip="+ zip_c'
                window.open(redirect,'blank');
            }
            if (myform.tx1.value == ""){

                document.getElementById('valid_text').innerHTML = "Please Enter A Valid Zip Code.";
                return false;
            }
            if ((myform.tx1.value.length < 5) || (myform.textfielda.value.length > 20) || (myform.tx1.value.length == 0))
            {
                document.getElementById('valid_text').innerHTML = "Please Enter A Valid Zip Code.";
                return false;
            }

            return false;
        }

    </script>  

这是我的表格:

<form onSubmit="return redirect();" id="myform" name="myform"  method="" action="">                    
                <div id="tx_a"><div class="inText">Zip Code:</div><input type="text" class="numeric" name="tx1" id="tx1" maxlength="5" /></div>
                <div id="button"><input type="submit" name="submit" value="" /></div>
                <input type="hidden" id="sid" name="sid" value="<?php echo $sid; ?>" />
</form>  

我自己找到了解决方案:

    function redirect(){
            var zip_c = document.myform.tx1.value;
            var sid = document.myform.sid.value;
            if ((myform.tx1.value.length < 5) || (myform.tx1.value.length == 0))
            {
                document.getElementById('valid_text').innerHTML = "Please Enter A Valid Zip Code.";
                return false;
            }
            if ((myform.tx1.value.length == 5) || (myform.textfielda.value.length > 20)){
                /* if (zip_c == "")
                {
                   return false;
                }
                 */
                var redirect = "http://www.yourcarinsurancefinders.com/lowest-rates/quotes/ppv_hc01/src/results/index.php?zip="+ zip_c+"&sid="+sid; //Edint redirection link here, but dont change '?zip="+ zip_c'
                window.open(redirect,'blank');
            }                                                    
        }

1 个答案:

答案 0 :(得分:1)

如果你想重定向,你可以返回true,它会将它提交给表单的动作。 或者您可以使用window.location = 'whatever.com';