简体   繁体   English

如何制作自定义验证 javascript 表单?

[英]How do I make a custom validation javascript form?

I want to make the submit button on my form so that when the user clicks on it, it comes up with an alert box telling them what they haven't filled in (which would be done with JavaScript) how do I go about making this?我想在我的表单上制作提交按钮,这样当用户点击它时,它会出现一个警告框,告诉他们他们没有填写的内容(这将使用 JavaScript 完成)我该如何制作?

I want to use the "Flip in & scale" from http://tympanus.net/Development/ModalWindowEffects/ as the custom alert box (which then will pop up what the user hasn't filled in)我想使用http://tympanus.net/Development/ModalWindowEffects/ 中的“Flip in & scale”作为自定义警报框(然后会弹出用户未填写的内容)

Also I want it so it checks that the user have put in a correct email in the right format and also that they have put their name (which shouldn't include numbers)我也想要它,所以它会检查用户是否以正确的格式输入了正确的电子邮件,以及他们是否输入了他们的姓名(不应包含数字)

Thanks.谢谢。

<form id="myForm" method="post" action="/home"> // change the 'action' attribute to the link you want to post the form
    <input type="text" id="inputArea" onfocus="myFunc()">
    <span id="error" class="hide">Please enter a value</span>
    <button type="submit" id="myButton">Submit</button>
</form>

<script>
function myFunc(){
   var input = document.getElementById('inputArea');
   var button = document.getElementById('myButton');
   var span = document.getElementById('error');
   if(input.value == ''){
       span.className = errorMessage;
   } else if(input.value != ''){
        span.className = hide;
   }
}
</script>


    css:


.hide {display:none;}
.errorMessage {color:red;}

You could even be more specific for the input by using regular expressions but that's a different story!您甚至可以通过使用正则表达式对输入进行更具体的处理,但那是另一回事了!

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

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