如何使用Javascript验证我的Texbox [英] How Do I Validate My Texbox Using Javascript
问题描述
我有一个电子邮件文本框(必填字段)和一个按钮。我希望每当用户输入他的邮件ID并点击按钮时,就会出现一个JavaScript弹出窗口。
我的问题是当我点击按钮时,弹出窗口出现之前必填字段消息
我希望编写的JavaScript代码如果文本框为空则会显示请输入电子邮件ID验证消息,如果用户提供他的电子邮件ID,然后弹出窗口将出现;否则没有。
请帮帮我
这是我的HTML
< < span class =code-leadattribute> div class = input-append >
< 输入 type = email id = email 名称 = 电子邮件跨度> 占位符 = 您的电子邮件 / >
< 按钮 class = subscribe-button btn type = submit onclick = myFunction() > SUBSCRIBE < / button >
< / div >
这是我的javascript
< script type = text / javascript >
函数myFunction(){
alert(你已经完成了);
}
< / script >
你需要写这样的东西:
function myFunction(){
var enteredEmail = document.getElementById(' email')。 value ;
if (enteredEmail == ){
alert( 请输入电子邮件ID);
}
else {
alert( 你已经完成了);
}
}
有很多教程可供谷歌使用。
请参阅此链接:电子邮件地址验证使用正则表达式的JavaScript
只需调用以下方法
function checkEmail(){
var email = document.getElementById(' txtEmail 跨度>);
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-azA -Z0-9 \ - ])+ \。)+([a-zA-Z0-9] { 2 , 4 跨度>})+
I have an email textbox (required field) and a button. I want that whenever a user puts his mail id and clicks the button a JavaScript popup should appear.
My problem is when I click the button, that popup is appearing before the required-field msg.
I want JavaScript code written in a way that if textbox is empty then it will show a "please enter email id" validation message and if user gives his email id then that popup will appear; otherwise not.
pls help me out
this is my HTML
<div class="input-append">
<input type="email" id="email" name="email" placeholder="Your Email"/>
<button class="subscribe-button btn" type="submit" onclick="myFunction()">SUBSCRIBE</button>
</div>
and this is my javascript
<script type="text/javascript">
function myFunction() {
alert("You have done it");
}
</script>
You need to write something like this:
function myFunction() { var enteredEmail = document.getElementById('email').value; if (enteredEmail == "") { alert("Please enter email id"); } else { alert("You have done it"); } }
There are a lot of tutorials available just google for it.
Refer this link : Email Address Validation in JavaScript using Regular Expressions
Just call the below method
function checkEmail() { var email = document.getElementById('txtEmail'); var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+
这篇关于如何使用Javascript验证我的Texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!