当我从输入文本中取消该值后,我正在执行JavaScript警报。但是我希望在输入文本中提供JavaScript警报值之后。
这是我的代码
<script>
function validateform() {
var email = $('#txt_emailID').val();
if (email == null || email == "") {
alert("Email Should Be Complusory");
return false;
}
var atposition = email.indexOf("@");
var dotposition = email.lastIndexOf(".");
if (atposition < 1 || dotposition < atposition + 2 || dotposition + 2 >= email.length) {
alert("Please enter a valid e-mail address...!!!");
return false;
}
}
</script>
<label for="fname">Name</label>
<input type="text" id="txt_name" name="txt_name" placeholder="Your Name.." />
<label for="lname">EmailID</label>
<input type="text" id="txt_emailID" name="email" placeholder="Your EmailID.." />
<label for="lname">Subject</label>
<input type="text" id="txt_subject" name="txt_subject" placeholder="Your Subject.." />
<input type="submit" id="submit_mail" value="Send" />
<script>
$(function () {
$(document).on("click", "#submit_mail", function () {
if (validateform() == false) {
document.getElementById("txt_name").value = $('#txt_name').val();
document.getElementById("txt_subject").value = $('#txt_subject').val();
}
else {
}
});
});
我不知道我在哪里遇到问题。
答案 0 :(得分:3)
您应该阻止提交表单的提交按钮的默认操作。
function validateform() {
var email = $('#txt_emailID').val();
if (email == null || email == "") {
alert("Email Should Be Complusory");
return false;
}
var atposition = email.indexOf("@");
var dotposition = email.lastIndexOf(".");
if (atposition < 1 || dotposition < atposition + 2 || dotposition + 2 >= email.length) {
alert("Please enter a valid e-mail address...!!!");
return false;
}
}
$(function() {
$(document).on("click", "#submit_mail", function(e) {
if (validateform() == false) {
e.preventDefault();
document.getElementById("txt_name").value = $('#txt_name').val();
document.getElementById("txt_subject").value = $('#txt_subject').val();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="fname">Name</label>
<input type="text" id="txt_name" name="txt_name" placeholder="Your Name.." />
<label for="lname">EmailID</label>
<input type="text" id="txt_emailID" name="email" placeholder="Your EmailID.." />
<label for="lname">Subject</label>
<input type="text" id="txt_subject" name="txt_subject" placeholder="Your Subject.." />
<input type="submit" id="submit_mail" value="Send" />