简体   繁体   English

具有int和selectbox的Js验证

[英]Js Validation with int and selectbox

I'm asking how to validate correctly my form. 我在问如何正确验证我的表格。

I have a selectBox (named persorecepteur). 我有一个selectBox(名为persorecepteur)。 If the selected item worth 0, the JS will check the 4 fields (all are integers and I have check the number of digits) (their names are establishment, bank, account, key). 如果所选项目的价值为0,则JS会检查4个字段(均为整数,我已经检查了位数)(其名称为营业所,银行,帐户,密钥)。

At the end of the form, I have 2 fields that MUST be validated too (a double and a text) 在表格末尾,我有2个字段也必须进行验证(双精度和文本)

So this is what I made : 这就是我所做的:

<script type="text/javascript">
function valider(){
    var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
        var ck_double = /^\d{3}\.\d{2}$/;


        var value = "0";
        if (myForm.persorecepteur.options[myForm.persorecepteur.selectedIndex].value == value ){

            // mist be 14701
            value = "14701";
            if (document.myForm.stablishment.value != value ) {
                document.myForm.establishment.style.backgroundColor="red";
        alert("Must be 14701");
                return false;
            }
            value = "00000";
            if (document.myForm.bank.value != value ) {
                document.myForm.bank.style.backgroundColor="red";
        alert("Must be 00000");
                return false;
            }

            if (document.myForm.account.value.length() != 11 ) {
                document.myForm.account.style.backgroundColor="red";
        alert("You must enter 11 digits");
                return false;
            }

            if (document.myForm.key.value.length() != 2 ) {
                document.myForm.key.style.backgroundColor="red";
        alert("You must enter 2 digits");
                return false;
            }

        }


        if (!ck_double.test(document.myForm.amount.value)) {
        document.myForm.amount.style.backgroundColor="red";
                alert("You must enter the sum like that : abc.de ");
                return false;
            }
    if (!ck_name.test(document.myForm.description.value)) {
        document.myForm.description.style.backgroundColor="red";
        return false;
            }

        return true;
}
</script>

But first I don't have to make "value" isn't it? 但是首先我不必做出“价值”,不是吗? Can I check the lenght like I made with .value.length() ? 我可以像使用.value.length()一样检查长度吗?

Thanks to help me, I'm a beginner in JS ... 感谢帮助我,我是JS的初学者...

EDIT 编辑

<form name="myForm" action="performtransfert" method="POST" onsubmit="return valider();">

        <table width="100%" border="1" cellspacing="0" cellpadding="0">
    <tr>
            <td>Selectionnez un compte en banque a debiter : </td>
            <td>
                <SELECT name="debiteur" size="1">

                    <option value="4">compte facturier (Current acount) - 100.0</option>

                    <option value="7">livret vert (Spare acount) - 1379.98</option>

                </SELECT>
            </td>
        </table>
        <br /><br />

        <table width="100%" border="1" cellspacing="0" cellpadding="0">
    <tr>
            <td><SELECT name="persorecepteur" size="1">
                    <option value="0">Selectionnez l'un de vos comptes si c'est le destinataire

                    <OPTION value="4">compte facturier (Current acount) - 100.0</OPTION>

                    <OPTION value="7">livret vert (Spare acount) - 1379.98</OPTION>

                </SELECT>
                <br /><br />
                Sinon, veuillez remplir le BBAN : <INPUT type=text name="establishment" size="5"> <INPUT type=text name="bank" size="5"> <INPUT type=text name="account" size="13"><INPUT type=text name="key" size="1"> 
            </td>
        </tr>
        </table>



        <br /><br />

        <table width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr>
                <td>Somme à verser : </td>
                <td><INPUT type=text name="amount" size="6"></td>
                <td>Desctiption : </td>
                <td><INPUT type=text name="description" size="45"></td>
            </tr>
        </table>

        <br /><br />
        <input type="submit" value="Valider le payement" />

    </form>

I suppose you are asking if you have to declare the variable value in your question But first I don't have to make "value" isn't it? 我想您是在问是否必须在问题中声明变量值, But first I don't have to make "value" isn't it? ? No, you don't have to, you should be able to compare the actual value itself, like this - document.myForm.bank.value == "0" 不,您不必这样做,您应该能够比较实际值本身,就像这样document.myForm.bank.value == "0"

And yes, you can check the length with document.myForm.account.value.length and not document.myForm.account.value.length() 是的,您可以使用document.myForm.account.value.length而不是document.myForm.account.value.length()检查长度。

I would recommend you start using (learning) jQuery validation. 我建议您开始使用(学习)jQuery验证。 It makes things much much easier and neater... 它使事情变得更加轻松和整洁。

The following simple jQuery validation will work for your form... 以下简单的jQuery验证将适用于您的表单...

$(document).ready(function(){

    $("#myForm").validate({
        rules: {
            amount: {
                required: true,
                number: true
            },
            description: {
                required: true
            },
            establishment: {
                required: function(element) {
                    return $("#persorecepteur").val() == '0'
                },
                number: true
            },
            bank: {
                required: function(element) {
                    return $("#persorecepteur").val() == '0'
                },
                number: true
            },
            account: {
                required: function(element) {
                    return $("#persorecepteur").val() == '0'
                },
                number: true
            },
            key: {
                required: function(element) {
                    return $("#persorecepteur").val() == '0'
                },
                number: true
            }

        },
        messages: {
            amount: {
                required: "Needs entry",
                number: "only numbers allowed"
            },
            description: "Needs entry",
            establishment: {
                required: "Have to enter",
                number: "only numbers allowed"
            },
            bank: {
                required: "Have to enter",
                number: "only numbers allowed"
            },
            account: {
                required: "Have to enter",
                number: "only numbers allowed"
            },
            key: {
                required: "Have to enter",
                number: "only numbers allowed"
            },
        }
    });


    $('#myForm').submit(function() {
        if($("#persorecepteur").val() == '0') {
            if($("#establishment").val() !="" && $("#establishment").val() != "14701") {
                alert("Establishment must be 14701");
                return false;
            } else if($("#bank").val() != "" && $("#bank").val() != "00000") {
                alert("Must be 00000");
                return false;
            } else if($("#account").val() != "" && $("#account").val().length != 11) {
                alert("You must enter 11 digits");
                return false;
            } else if($("#key").val() != "" && $("#key").val().length != 2) {
                alert("You must enter 2 digits");
                return false;
            } 
        }
    });
 });

You will have to declare id's for your form fields for the above code to work. 您必须为表单字段声明id才能使上述代码正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM