I'm a newbee in javascript and i need to refresh page when script is done:
<script type="text/javascript">
function validateForm() {
var x = document.forms["myForm"]["Hostname"].value;
if (x == "" || x == null) {
alert("Name must be filled out");
return document.location.reload(true);
}
}
</script>
But i don't understand how to make it work.
try it
window.location.reload(true);
<script type="text/javascript">
function validateForm() {
var x = document.forms["myForm"]["Hostname"].value;
if (x == "" || x == null) {
alert("Please specify a FQDN of your host");
window.location.reload(true);
return false;
</script>
This one works fine. Thanks to all
Related
I want to implement 3 attempts login form using javascript prompt() method. it shows You have left 2 attempt and ddnt check for the next attempt. how to solve this?
here is my script
<script type="text/javascript">
var attempt = 3;
var userName=prompt("Username","");
var passWord=prompt("Password","");
if (userName+passWord=="john2541798432" || userName+passWord=="tim3591798752")
{
alert('success');
}
else
{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
if( attempt == 0)
{
alert('you are blocked');
}
}
</script>
<script type="text/javascript">
var loggedin = false;
var attempt = 3;
while(!loggedin && attempt > 0){
var userName=prompt("Username","");
var passWord=prompt("Password","");
if (userName+passWord=="john2541798432" || userName+passWord=="tim3591798752")
{
loggedin = true;
alert('success');
}
else
{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
if( attempt == 0)
{
alert('you are blocked');
}
}
}
</script>
According to your script, I think you can add a hidden input field and update the value of hidden field to verify the maximum attempt. Please see the below script
<input type="hidden" id="attempt" value="3">
<script type="text/javascript">
while(1)
{
var userName=prompt("Username","");
if(userName != "")
var passWord=prompt("Password","");
var attempt = document.getElementById("attempt").value;
if(userName != "" && passWord != "")
{
if (userName+passWord=="john2541798432" || userName+passWord=="tim3591798752")
{
alert('success');
break;
}
else
{
document.getElementById("attempt").value = attempt -1;
alert("You have left "+(attempt-1)+" attempt;");
attempt = attempt - 1;
if( attempt == 0)
{
alert('you are blocked');
break;
}
}
}
}
</script>
I just uploaded the file structure as it is to my EC2 instance. Same form file and same javascript validation file.
Validation works perfectly on localhost but not on EC2 and I am unable to figure out what is going wrong. I checked the directory structure, hrefs and links and they look ok to me.
Following is the Javascript file:
//Calling the validate function when user submits form
window.load = function() {
var myForm = document.getElementById('myForm');
myForm.onsubmit = function(e) {
return validate();
}
}
//Validation funtion
function validate() {
var product_name = document.forms["myForm"]["fname"].value;
var name = document.forms["myForm"]["pname"].value;
var email = document.forms["myForm"]["email"].value;
var phone = document.forms["myForm"]["phone"].value;
var price = document.forms["myForm"]["price"].value;
var date22 = document.forms["myForm"]["date22"].value;
var description = document.forms["myForm"]["description"].value;
var d = new Date();
var values=date22.split("-");
makeWhite();
if (product_name==null || product_name == "" ) {
makeRed('fname');
alert("Enter Product Name");
return false;}
else if (name == null || name == ""|| isNaN(name) == false) {
makeRed('pname');
alert("Enter valid name");
return false;
}
else if(email == '' || email.indexOf('#') == -1 || email.indexOf('.') == -1)
{
makeRed('email');
alert("Insert valid Email Address");
return false;
}
else if(phone == ''|| phone <1000000000 || phone >9999999999){
makeRed('phone');
alert("Enter valid phone number");
return false;
}
else if(price == ''|| price <0 || price >9999999999){
makeRed('price');
alert("Enter valid cost");
return false;
}
else if(description == ''|| description == null){
makeRed('description');
alert("Enter a description");
return false;
}
else if(values[0]=="" || values[0]==null||values[0]>d.getFullYear() || values[1]> 1+d.getMonth() || values[2]>d.getDate()){
makeRed('date22');
alert("Please Check Date");
return false;
}
}
//Function to make invalid input fields red
function makeRed(inputDiv){
var div= document.getElementById(inputDiv);
div.style.backgroundColor="#FFA07A";
div.style.border = "2px solid #FF0000 ";
return false;
}
//Function to clear fields when input is valid
function makeWhite(){
var divList = document.querySelectorAll(".inputField");
for (var i=0;i<divList.length;i++){
divList[i].style.backgroundColor="#FFFFFF";
divList[i].style.border = "1px solid #BDBDBD";
}
return false;
}
References to the JS file:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/carouFredSel.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/js_validation.js"></script>
Have you confirmed that the files are actually loading? I could see an issue with js/jquery.js vs /js/jquery.js
I'm trying to add validation before form submit. There's an alternative input field, let's call this "Optional" and there's checkboxes "Checkbox1", "Checkbox2", "Checkbox3". When user choose one from this checkboxes, then he must fill input "Optional", if it is empty then he can't submit form and get's some pop-up alert with info to fill this.
I have something like this:
<script>
function validateForm() {
var x = document.forms["form"]["nrumowy"].value;
if (x == null || x == "") {
alert("Numer umowy musi zostać uzupełniony.");
return false;
}
}
</script>
But this input field "nrumowy" must be filled in all situations, so everything okay, and now I want add something similiar but only when one of "Checkbox1", "Checkbox2", "Checkbox3" are checked.
Following code will work instantly while filling the form.
jquery code
<script>
function validateForm() {
var x = document.forms["form"]["nrumowy"].value;
if (x == null || x == "") {
alert("Numer umowy musi zostać uzupełniony.");
return false;
}
var op = document.forms["form"]["optional"].value;
if ($("input[type=checkbox]:checked").length === 0 && op==""){
alert("you must fill Optional field");
return false;
}
}
</script>
If you want to add something similiar but only when one of "Checkbox1", "Checkbox2", "Checkbox3" are checked. Then see if following code helps you.
function validateForm() {
if(document.getElementById("Checkbox1").checked ||
document.getElementById("Checkbox2").checked ||
document.getElementById("Checkbox3").checked ||
{
var x = document.forms["form"]["nrumowy"].value;
if (x == null || x == "") {
alert("Numer umowy musi zostać uzupełniony.");
return false;
}
}
}
You should simply get the values of these checkboxes before that, and if one of them is not checked, exit the function.
function validateForm() {
var frm = document.forms["form"];
if (!frm["Checkbox1"].checked && !frm["Checkbox2"].checked && !frm["Checkbox3"].checked) return;
var x = frm["nrumowy"].value;
if (x == null || x == "") {
alert("Numer umowy musi zostać uzupełniony.");
return false;
}
}
Ok, thanks for your answers, I got now this:
<script>
function validateForm() {
var frm = document.forms["form"];
if (!frm["check_list1"].checked && !frm["check_list2"].checked && !frm["check_list3"].checked) return;
var x = frm["tematyka"].value;
if (x == null || x == "") {
alert("Tematyka artykułu musi zostać uzupełniona.");
return false;
}
}
</script>
<script>
function validateForm2() {
var x2 = document.forms["form"]["nrumowy"].value;
if (x2 == null || x2 == "") {
alert("Numer umowy musi zostać uzupełniony.");
return false;
}
}
</script>
HTML:
<form name="form" method="POST" action="preview.php" onsubmit="return (validateForm() & validateForm2());">
And alerts works, but after user click OK then he goes to preview.php page. When there's only one onsubmit function then this works great, but when I try to combine this two functions then there is a some error.
I just need help on how to pass multiple 'name' elements in my function that validates a form submission.
I have this and works fine.
var x = document.forms["myForm"]["FirstName"].value;
if (x == null || x == "")
{
alert("asdf");
return false;
}
tried this but didnt work.
var x = document.forms["myForm"]["FirstName"+"LastName"].value;
if (x == null || x == "")
{
alert("asdf");
return false;
}
Any help with the syntax on how to do so would be appreciated.
What you are trying makes no sense.
var x=document.forms["myForm"]["FirstName"+"LastName"].value;
would get the value from an element called FirstNameLasteName.
So you probably meant this:
var x=document.forms["myForm"]["FirstName"].value + document.forms["myForm"]["LastName"].value;
You meant this:
var x=document.forms["myForm"]["FirstName"].value + " " +
document.forms["myForm"]["LastName"].value;
if (x==null || x=="")
{
alert("asdf");
return false;
}
I am trying to figure out a simple way to limit the amount of attempts on a field(s) through a while loop in javascript. Just doesn't seem to want to loop but the validation is still working. Here is the code I was trying to use:
function validateForm() {
x=document.forms["Form"]["name"].value;
var i=0;
var sum=0;
do {
sum += i;
i++;
}
while (i < 3)
if (x==null || x=="") {
alert("First name must be filled out");
return false;
}
}
Thanks in advance for any ideas.
You will have to store the variable outside of the function:
var numOfTries = 0;
function validateForm() {
if(++numOfTries > 3) {
alert('You have reached the maximum number of tries!');
location.replace("error_page.html"); // do something about it
}
x = document.forms["Form"]["name"].value;
if (x == null || x == "") {
alert("First name must be filled out");
return false;
}