No refresh on submit - html

I've got a site with a form.
The value of the input should be a specific value. This works:
$('button').on('click', function () {
var val1 = $("#teller").val();
var val2 = $("#noemer").val();
if (val1 == 3 && val2 == 10)
alert("Goed gedaan! je hebt de vraag goed beantwoord. Je kunt verder met je avontuur!");
else
alert("Helaas... Dit is niet het goede antwoord. Probeer het nog eens!");
});
But when I submit the page will refresh, change my url and refresh the page so the page starts at the beginning again... This is the site: http://stilld.nl/brrreuk/
I don't want this to happen. I want the code to check if the value's given are correct, but then I want the site to stay where it is.
Hope you can help.
this is the html:
<form id="form_1">
<fieldset>
<p>
<input type="text" class="input_required" id="teller" name="teller" maxlength="2" class="valid" aria-invalid="false" onkeydown="return isNumber(event);" placeholder="?" onfocus="this.placeholder = ''" onblur="this.placeholder = '?'">
<label for="teller"></label>
</p>
<hr noshade size=3>
<p>
<input type="text" class="input_required" id="noemer" name="noemer" maxlength="2" class="valid" aria-invalid="false" onkeydown="return isNumber(event);" placeholder="?" onfocus="this.placeholder = ''" onblur="this.placeholder = '?'">
<label for="noemer"></label>
</p>
<p>
<button class="submit" type="submit" value="controleer">Controleer</button>
</p>
</fieldset>
</form>

Here you go. Simply change the submit to a button.
The JavaScript:
$('button').on('click', function(){
var val1 = $("#teller").val();
var val2 = $("#noemer").val();
if(val1 == 3 && val2 == 10)
alert("Goed gedaan! je hebt de vraag goed beantwoord. Je kunt verder met je avontuur!");
else
alert("Helaas... Dit is niet het goede antwoord. Probeer het nog eens!");
});
And the HTML
<p>
<input type="text" class="input_required" id="teller" name="teller" maxlength="2" class="valid" aria-invalid="false" onkeydown="return isNumber(event);" placeholder="?" onfocus="this.placeholder = ''" onblur="this.placeholder = '?'">
<label for="teller"> </label>
</p>
<hr noshade size=3>
<p>
<input type="text" class="input_required" id="noemer" name="noemer" maxlength="2" class="valid" aria-invalid="false" onkeydown="return isNumber(event);" placeholder="?" onfocus="this.placeholder = ''" onblur="this.placeholder = '?'">
<label for="noemer"> </label>
</p>
<p>
<button type="button" value="controleer"> Controleer </button>
</p>

List to the form's submit event instead of the button's click event:
$('form').on('submit', function(e) {
e.preventDefault();
// do stuff
// $(this).trigger('submit') if everything's ok
});
NOTE: The e.preventDefault(); line is important!

Related

Cant update a entry in Angular

When I try to update an entry i don't have any errors but nothing happens
I dont have much idea of angular, i am sorry.
This is my html
<div class="modal-body">
Id del Envío: <input type="text" placeholder="Id del envío" id="idE" class="form-control" value="{{envioactual.idEnvio}}" disabled> <br>
Nombre del Destinatario: <input type="text" placeholder="Nombre Cliente" id="inombreDestinatario" class="form-control" value="{{envioactual.nombreDestinatario}}"> <br>
Dirección de Entrega <input type="text" placeholder="Dirección Completa" id="idireccionCompleta" class="form-control" value="{{envioactual.direccionCompleta}}"> <br>
DNI del receptor <input type="text" placeholder="DNI del receptor" id="iDNI" class="form-control" value="{{envioactual.dninif}}"> <br>
Código Postal <input type="text" placeholder="Código Postal" id="icodigoPostal" class="form-control" value="{{envioactual.codigoPostal}}"> <br>
Número de intentos de entrega <input type="text" placeholder="Número de intentos de entrega" id="inumIntentosEntrega" class="form-control" value="{{envioactual.numIntentosEntrega}}"> <br>
Estado del envío <input type="text" placeholder="Estado del envío" id="iidEstadoEnvio" class="form-control" value="{{envioactual.idEstadoEnvio}}"> <br>
Id Del Cliente<input id="prodId" id="clienteId" class="form-control" value="{{envioactual.idCliente}}" disabled> <br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" (click)="actualizarEnvio()">Actualizar</button>
This my 'envios' component
envioactual : Envio;
actualizarEnvio()
{
let idEnvio = parseInt((document.getElementById('idE') as HTMLInputElement).value);
let nombreDestinatario = (document.getElementById('inombreDestinatario') as HTMLInputElement).value;
let direccionCompleta = (document.getElementById('idireccionCompleta') as HTMLInputElement).value;
let codigoPostal = (document.getElementById('icodigoPostal') as HTMLInputElement).value;
let numIntentosEntrega = parseInt((document.getElementById('inumIntentosEntrega') as HTMLInputElement).value);
let idEstadoEnvio = (document.getElementById('iidEstadoEnvio') as HTMLInputElement).value;
let dninif = (document.getElementById('iDNI') as HTMLInputElement).value;
let idCliente = (document.getElementById('idCliente') as HTMLInputElement).value;
const contenido =
{
idEnvio, nombreDestinatario, direccionCompleta, codigoPostal, numIntentosEntrega, dninif, idEstadoEnvio, idCliente
}
console.table(contenido)
this.conexion.actualizarEnvio(contenido).subscribe(xx => this.envioactual = xx);
}
}
This my conexion service
actualizarEnvio(conexion : Envio)
{
const path = `${this.api}/actualizarEnvio/${conexion.idEnvio}`;
return this.http.put<Envio>(path, conexion);
}
This is the interface
export interface Envio
{
idEnvio : number;
nombreDestinatario: string;
dninif: string;
codigoPostal: string;
direccionCompleta:string;
idEstadoEnvio: string;
numIntentosEntrega: number;
idCliente: string;
}
And the relationship on database
[1]: https://i.stack.imgur.com/CodiV.png

Issue in html code for redirect user to url after is logged

Hi I’m html learner and I have an issue with my code but I don’t know what’s the problem
The prob is weird , when i enter the right pass in only have my alert « Le mot de passe est correct » but the page is just reload and not move to https://mycool-url.com
This is a simple html code it’s for learn password
<title>Authentification</title>
</head>
<body>
<div class="main">
<p class="sign" align="center">Veuillez vous identifier</p>
<form class="login">
<input class="pass" type="password" align="center" id="password" placeholder="Mot de Passe" type="password" name="pswrd">
<input class="submit" align="center" type="submit" onclick="checkPassword()" value="Login" />
</form>
</div>
<script>
function checkPassword() {
if (document.getElementById('password').value == 'ZhFAIHhyRvBaZ') {
alert('Mot de passe correct')
location.replace('https://www.mycool-url.com');
} else {
alert('Mot de passe incorrect');
return false;
}
}
</script>
</script>
</body>
The form is submitting. If you don't want to submit a form, change the button type from type="submit" to type="button":
<input class="submit" align="center" type="button" onclick="checkPassword()" value="Login" />
If you're not using a "form" at all, you can even just remove the <form> element entirely, which would prevent accidental form submission.
As an aside, it's also worth noting that any user can see the "password" or go directly to the target URL so this authentication mechanism doesn't secure anything.
The form is submitting each time. If you don't want to submit the form, you can use the following things for prevent form submission in your cause.
<input class="submit" align="center" type="submit" onclick="checkPassword(); return false;" value="Login"/>
Change the javascript function checkPassword() to return false all the time.
function checkPassword(){
if(document.getElementById('password').value == 'ZhFAIHhyRvBaZ'){
alert('Mot de passe correct');
window.location.href='https://www.mycool-url.com';
} else {
alert('Mot de passe incorrect');
return false;
}
return false;
}
and call it in onclick like this onclick="return checkPassword();"
<input class="submit" align="center" type="submit" onclick="return checkPassword()" value="Login"/>
It will also prevent the form from submitting.
Replace location.replace('https://www.mycool-url.com'); by window.location.replace("https://www.mycool-url.com");

How to validate number fields with template driven forms in Angular?

Is there a way to validate the numerical value of input field using template driven forms in Angular 11?
To give a bit of context, I'm building a memory card game using Angular 11 and I'd like to make sure that the player can't select a maxFlipCount that is inferior to the number of cards. I have been struggling to make this work using the min attribute on an input tag. Maybe there's another that I don't know of that doesn't use the min attribute.
How could I achieve this?
I'm trying to apply this validation to the last input tag, before the submit button in my template code. This is what I have so far:
<form #gameOptionsForm="ngForm" (ngSubmit)="startGame()" novalidate>
<!-- mode de jeu -->
<div class="field">
<label class="label">Mode de jeu</label>
<div class="control">
<div class="select" [class.is-danger]="
gameMode.invalid && (gameMode.dirty || gameMode.touched)
">
<select name="gameMode" [(ngModel)]="optionsForm.gameMode" #gameMode="ngModel" required>
<option value="1">1 joueur</option>
<option value="2">2 joueurs</option>
</select>
</div>
</div>
<p class="help is-danger" *ngIf="gameMode.invalid && (gameMode.dirty || gameMode.touched)">
Vous devez choisir un mode jeu
</p>
</div>
<!-- nom du premier joueur -->
<div class="field" *ngIf="optionsForm.gameMode == 1 || optionsForm.gameMode == 2">
<label class="label">Nom du joueur 1</label>
<div class="control">
<input [class.is-danger]="
player1.invalid && (player1.dirty || player1.touched)
" class="input" type="text" name="player1" placeholder="Joueur 1" #player1="ngModel" [(ngModel)]="optionsForm.player1" required />
</div>
<p class="help is-danger" *ngIf="player1.invalid && (player1.dirty || player1.touched)">
Veuillez entrer un nom pour le joueur 1
</p>
</div>
<!-- nom du deuxieme joueur -->
<div class="field" *ngIf="optionsForm.gameMode == 2">
<label class="label">Nom du joueur 2</label>
<div class="control">
<input [class.is-danger]="
player2.invalid && (player2.dirty || player2.touched)
" class="input" type="text" name="player2" placeholder="Joueur 2" #player2="ngModel" [(ngModel)]="optionsForm.player2" required />
</div>
<p class="help is-danger" *ngIf="player2.invalid && (player2.dirty || player2.touched)">
Veuillez entrer un nom pour le joueur 2
</p>
</div>
<!-- taille de la grille -->
<div class="field">
<label class="label">Nombre de cartes</label>
<div class="control">
<div class="select" [class.is-danger]="
gridSize.invalid && (gridSize.dirty || gridSize.touched)
">
<select name="gridSize" [(ngModel)]="optionsForm.gridSize" #gridSize="ngModel" required>
<option value="4">4</option>
<option value="16">16</option>
<option value="36">36</option>
</select>
</div>
</div>
<p class="help is-danger" *ngIf="gridSize.invalid && (gridSize.dirty || gridSize.touched)">
Vous devez choisir un nombre de cartes
</p>
</div>
<!-- nombre maximal de coups -->
<div class="field">
<label class="label">Nombre maximal de coups</label>
<div class="control">
<input [class.is-danger]="
maxFlipCount.invalid && (maxFlipCount.dirty || maxFlipCount.touched)
" class="input" type="number" name="maxFlipCount" #maxFlipCount="ngModel" [(ngModel)]="optionsForm.maxFlipCount" required min="{{ optionsForm.gridSize }}" />
</div>
<p class="help is-danger" *ngIf="
maxFlipCount.invalid &&
(maxFlipCount.dirty || maxFlipCount.touched) &&
maxFlipCount.errors.required
">
Veuillez entrer un nombre maximal de coups
</p>
<p class="help is-danger" *ngIf="maxFlipCount.errors?.min">
Veuillez entrer un nombre supérieur ou égal à {{ optionsForm.gridSize }}
</p>
</div>
<button type="submit" [disabled]="!gameOptionsForm.form.valid">
Commencer
</button>
</form>
The min attribute does actually do it's job when I use the little arrows (automatically put there by the browser) on the side to increase/decrease the value, however the problem is when I type a value in manually, the field value can be whatever I want and it will not appear as being invalid.
Thank you for reading through this and suggesting any ideas.
I ended up finding the answer to my question. So, I used a custom directive to check that the value of the field was what I wanted.
This article explains it quite well: https://www.digitalocean.com/community/tutorials/angular-custom-validation

How to change the href of a button?

I have a this:
<input type="submit" class="inputlogin submit" name="register" style="margin-top: 5px;" value="Maak mijn account en log in!">
and then it goes to register/register. I want that it goes to /me, how I can do this?
Musa, welcome to stack overflow!
The submit button does exactly what its name says: It submits a form. Now where the form is submitted to is not a property of the submit button, but of the form.
You will find, that the submit button is part of a <form action="..."> .. </form> construct. By changing the action property of the form, you can point it to another URL, e.g. to ' /me'
The page the input refers to is defined in the action attribute of the <form> not the <input> itself.
<form action="/me" method="get">
Your code must be like this:
<form action="/me" method="post">
<input type="submit" class="inputlogin submit" name="register" style="margin-top: 5px;" value="Maak mijn account en log in!">
</form>
I'm really confused right now. So, I have <input type="submit" class="inputlogin submit" name="register" style="margin-top: 5px;" value="Maak mijn account en log in!"> and that I need to change to: <input action="/me" method="get" type="submit" class="inputlogin submit" name="register" style="margin-top: 5px;" value="Maak mijn account en log in!"> maybe I needed this to say earlier, but it's a registration form, so it needs to be checked if everything is filled in, and it needs to get in the database.
So, I see now that I already had the right code, but it goes to /register/me.. I'm really ashamed...
Another approach would be this:
<input type="button" class="inputlogin submit" style="margin-top: 5px;" value="Maak mijn account en log in!" onclick="parent.location='/me'">
This makes use of onclick and will work without a form. Remember that it doesn't POST a form
<div class="text" style="font-weight:bold;padding:10px;"><?php if(isset($template->form->error)) { echo '<div class="error-messages-holder"><ul><li><p class="error-message">'.$template->form->error.'</p></li></ul></div>'; } ?>
<form method="post" action="{url}/me" autocomplete="off">
<label for="index_rusername">Neem een unieke nickname:</label><br />
<input type="text" placeholder=" Jouw gebruikersnaam " class="inputlogin" name="reg_username"><br />
<label for="index_rpassword">Kies een sterk wachtwoord:</label><br />
<input type="password" placeholder=" Jouw wachtwoord " class="inputlogin" name="reg_password"><br />
<label for="index_rconpassword">Typ je wachtwoord over:</label><br />
<input type="password" placeholder=" Jouw wachtwoord " class="inputlogin" name="reg_rep_password"><br />
<label for="index_remail">Jouw email adres:</label><br />
<input type="text" placeholder=" Jouw e-mail adres " class="inputlogin" name="reg_email"><br />
<input type="submit" class="inputlogin submit" name="register" style="margin-top: 5px;" value="Maak mijn account en log in!">
</form>
</div>
</div>
I have this code now. But if I press on the button, it doesn't register in the database, so all the configurated things doesn't come in in the table "users".

HTML textbox required on button click

<b>Gebruikersnaam:</b><br>
<input id="textbox" type="text" value="" size="25" required>
<a id="googleLink" href="##klik nu om member te kopen##"
onclick="this.href='https://websitelink.com?naam=' + encodeURIComponent(document.getElementById('textbox').value);">
<button>Koop: MEMBER</button>
</a>
I want: <input id="textbox" type="text" value="" size="25" required>
to be required when i click: <button>Koop: MEMBER</button>
like: if <input id="textbox" type="text" value="" size="25" required> value=""
its shows an alert like: "No username entered"
Fixed, Thx Lepanto
You need to cleanup your code first. not sure why you're using <a>. Anyway It would be pretty good to validate using onblur()
HTML code:
<input id="textbox" type="text" value="" size="25" required onblur="validateTextBox()">
JavaScript:
function validateTextBox() {
if (document.getElementById("textbox").value != "") {} else {
alert("Please enter a value");
}
}
Check this out in JSFiddle.
Of course, there is no such thing as required attribute in HTML tag textarea.
You have to create your own validation methods using Javascript. One of them is Validation plugin (which requires jQuery).
You have to write a JavaScript function to validation the textbox. Something like below
<script>
function doclick(){
if(document.getElementById('textbox').value != ''){
this.href='https://websitelink.com?naam=' + encodeURIComponent(document.getElementById('textbox').value);
}
else{
alert('No username entered');
}
}
</script>
And call this function in your HTML tag onclick
<b>Gebruikersnaam:</b><br>
<input id="textbox" type="text" value="" size="25" required>
<a id="googleLink" href="##klik nu om member te kopen##" onclick="doclick()">
<button>Koop: MEMBER</button>
</a>