简体   繁体   English

使用JavaScript时使用正则表达式验证表单

[英]validation form with regex while using Javascript

i'm trying to validate my bootstrap form with regex in javascript. 我正在尝试使用javascript中的正则表达式来验证我的引导表单。 I've started the javascript but don't know the right way to continue the validation with my regular expression. 我已经启动了javascript,但不知道正确的方式来继续使用我的正则表达式进行验证。 I'm trying to validate every input in my form before submitting it. 我正在尝试在提交表单之前验证表单中的每个输入。

If anyone could help me with my issue it would be appreciated. 如果有人可以帮助我解决我的问题,将不胜感激。

Thank you very much in advance. 提前非常感谢您。 In javascript no Jquery please 在JavaScript没有jQuery请

John Simmons 约翰·西蒙斯

HTML (This is my html bootstrap form) HTML(这是我的html引导程序表格)

<div class="col-md-6">
        <div class="well well-sm">
            <form class="form-horizontal" id="form" method="post" onsubmit="return validerForm(this)">
                <fieldset>
                    <legend class="text-center header">Contact</legend>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="lastName" name="LN" type="text" placeholder="Nom" autofocus class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="firstName" name="FN" type="text" placeholder="Prenom" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="email" name="email" type="text" placeholder="Courriel" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="phone" name="phone" type="text" placeholder="Téléphone" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <textarea class="form-control" id="message" name="Message" placeholder="Entrez votre message. Nous allons vous répondre le plus tôt que possible." rows="7"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12 text-center">
                            <button type="submit" class="btn btn-primary btn-lg">Submit</button>
                            <input class="btn btn-primary btn-lg" type="reset" onclick="clearForm()" value="Clear">
                        </div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>

Javascript (this is my javascript with my regexs, I was thinking about doing a function that would verify every value entered with the regex) Javascript(这是我的带有正则表达式的JavaScript,我在考虑做一个函数来验证用正则表达式输入的每个值)

var nameregex = /(^[A-Z][a-z]{1,24})+$/;
var emailregex= /^([A-Za-z])([A-Za-z0-9])+\@([a-z0-9\-]{2,})\.([a-z]{2,4})$/;


function validerForm(form) {
    window.onload = function(){
        document.getElementById('lastName').focus();
    }

    var valName = Formulaire.name.value;
    var valFirst = Formulaire.firstname.value;
    var valEmail = Formulaire.email.value;

    var nameValide = validationName(valName);
    var firstValide = validationFirstName(valFirst);
    var emailValide - validationEmail(valEmail);


}

function validationName(valName){
    if(nameregex.test(valName) == true){

    }else{

    }
}

function clearForm() {
    document.getElementById("form").reset();
}

您可以使用string.match()

i.e.: if (valEmail.match(emailregex)) { do stuff!; }

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

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