Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript时使用正则表达式验证表单_Javascript_Html_Forms_Validation_Bootstrap 4 - Fatal编程技术网

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

使用Javascript时使用正则表达式验证表单,javascript,html,forms,validation,bootstrap-4,Javascript,Html,Forms,Validation,Bootstrap 4,我试图用javascript中的正则表达式验证我的引导表单。我已经启动了javascript,但不知道用正则表达式继续验证的正确方法。我试图在提交表单之前验证表单中的每个输入 如果有人能帮我解决这个问题,我将不胜感激 事先非常感谢。 在javascript中,请不要使用Jquery 约翰·西蒙斯 HTML(这是我的HTML引导表单) 您可以使用string.match() 如何在无效的输入下添加消息错误?只需将否定与NOT(!)一起使用,因此使用上面相同的示例:if(!valEmail.matc

我试图用javascript中的正则表达式验证我的引导表单。我已经启动了javascript,但不知道用正则表达式继续验证的正确方法。我试图在提交表单之前验证表单中的每个输入

如果有人能帮我解决这个问题,我将不胜感激

事先非常感谢。 在javascript中,请不要使用Jquery

约翰·西蒙斯

HTML(这是我的HTML引导表单)

您可以使用string.match()


如何在无效的输入下添加消息错误?只需将否定与NOT(!)一起使用,因此使用上面相同的示例:
if(!valEmail.match(emailregex)){在此处处理;}
<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>
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();
}
i.e.: if (valEmail.match(emailregex)) { do stuff!; }