开发者

Javascript form validation and redirection

the problem of this validation and redirection comes when the input is shorter that 5 digits, (it should be 5 to be valid). If its 5 , its ok the user is redirected as it should be. As i told, problem comes when its not valid (shorter than 5). If you can suggest me how to fix it, i will be thankful ! Thanks in advance guys. here is the code:

        <script type="text/javascript">
        function redirect(){
            var zip_c = document.myform开发者_如何学Python.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>  

And here is my form :

<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>  

I found solution on my own :

    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');
            }                                                    
        }


If you want to redirect, you could just return true, and it will submit it to the form's action. Or you could use window.location = 'whatever.com';

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜