You can styling your own popup. Or use some plugins.
http://jquerybyexample.blogspot.com/2013/01/jquery-popup-window-tutorial-plugins.html
Or you can create some element on the page, that will showing the error messages. Write some styles and make it look awesome !
<div class="error-messages" style="display:none;"></div>
After the form sending, and checking errors, write this.
$(".error-messages").text("Some error").fadeIn();
Or you can make it empty and hide it, after a seconds or after user focus.
$(".error-messages").empty().fadeOut();
Answer from m1k1o on Stack OverflowVideos
You can styling your own popup. Or use some plugins.
http://jquerybyexample.blogspot.com/2013/01/jquery-popup-window-tutorial-plugins.html
Or you can create some element on the page, that will showing the error messages. Write some styles and make it look awesome !
<div class="error-messages" style="display:none;"></div>
After the form sending, and checking errors, write this.
$(".error-messages").text("Some error").fadeIn();
Or you can make it empty and hide it, after a seconds or after user focus.
$(".error-messages").empty().fadeOut();
You can insert a hidden div above your form, and show it instead of the alert
<div class="form-errors"></div>
$(".form-errors").text("The form is not complete").show();
Use setTimeout, when you click button I stop interval and timer function because it is success, and when button isn't clicked and if x variable isn't y timer and interval continues countDown, I used setInterval for understanding how it works, also I edited code , I might this is what you want
const input = document.querySelector("input")
const button = document.querySelector("button")
const textTimer = document.querySelector("p")
let number = 10
let x = ""
textTimer.innerHTML = number
button.addEventListener("click", ()=> {
x = input.value
if(x === "y"){
alert("success")
clearTimeout(timer)
clearInterval(interval)
}
console.log(x)
})
const interval = setInterval(()=> {
number--
textTimer.innerHTML = number
if(number <= 0){
clearInterval(interval)
}
}, 1000)
const timer = setTimeout(()=> {
if(x.length > 0){
alert("Success")
} else {
alert("There is a problem")
}
}, 10000)
<input type="text">
<button>Insert Value in x</button>
<p></p>
The solution is .setTimeout() which allows you to run the function, command ... after an estimated time.
/*
// ES6
const checkVal = () => {
const input = document.querySelector('input').value;
// Ternary Operator
input === 'yes' ? alert('yes') : alert('no');
};
*/
function checkVal() {
const input = document.querySelector('input').value;
if(input.toLowerCase() === 'yes') {
alert('yes')
} else {
alert('no')
};
};
// 2000 means 2k miliseconds, if you want to set it on 10 seconds, then 10000
document.querySelector('button').addEventListener('click', () => {
setTimeout(checkVal
, 2000
)}
);
<p>If you type *yes*, it will alert yes. But if you type anything else(not yes) it will show *no*.</p>
<input type='text'>
<button type='text'>Click</button>
Your code to display message is correct, problem here is that the form is submitted when the button is clicked. You need to prevent the default action when the condition is not fulfilled.
I would recommend you to use form's submit event, when false is returned it will prevent the default action (form being submitted).
<head>
<script>
function myFunction() {
var a = document.forms["login"]["uname"].value;
var b = document.forms["login"]["pwd"].value;
if (a == "" || b == "") {
error = "All fields must be entered.";
document.getElementById("errorid").innerHTML = error;
return false;
}
return true;
}
</script>
</head>
<body>
<form name="login" action="" onsubmit="return myFunction()">
<b>Enter username:</b>
<input type="text" name="uname" />
<br />
<b>Enter password:</b>
<input type="password" name="pwd" />
<br />
<p id="errorid"></p>
<input type="submit" />
</form>
</body>
</html>
You can add the required attribute to the input if the user must enter it in order to submit the form you can do this like :<input type='text' required>
In this way you dont have to worry about the user not filling the field. HTML5 will automatically take care of that. Try it
You can set the contents of any element with innerHTML. So instead of messing with a form input, you could just have this:
<div id="message"></div>
And set it via JavaScript like this:
document.getElementById('message').innerHTML = 'This message is awesome.';
And you would get this result:
<div id="message">This message is awesome.</div>
You could just use a styled span and set it's innerHtml.