| Subiectul anterior :: Subiectul următor |
| Autor |
Mesaj |
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Sâm Apr 19, 2008 1:58 pm Titlul subiectului: Verificare prin java script. |
|
|
Vreau sa fac validarea unui formular si nu reusesc in totalitate:
Am asa :
Cod:
validator.js
function checkForm() {
marca = document.getElementById("marca").value;
name = document.getElementById("name").value;
email = document.getElementById("email").value;
comment = document.getElementById("comment").value;
if (marca == "") {
hideAllErrors();
document.getElementById("marcaError").style.display = "inline";
document.getElementById("marca").select();
document.getElementById("marca").focus();
return false;
} else if (name == "") {
hideAllErrors();
document.getElementById("nameError").style.display = "inline";
document.getElementById("name").select();
document.getElementById("name").focus();
return false;
} else if (email == "") {
hideAllErrors();
document.getElementById("emailError").style.display = "inline";
document.getElementById("email").select();
document.getElementById("email").focus();
return false;
} else if (comment == "") {
hideAllErrors();
document.getElementById("commentError").style.display = "inline";
document.getElementById("comment").select();
document.getElementById("comment").focus();
return false;
}
return true;
}
function hideAllErrors() {
document.getElementById("marcaError").style.display = "none"
document.getElementById("nameError").style.display = "none"
document.getElementById("emailError").style.display = "none"
document.getElementById("commentError").style.display = "none"
}
si
Cod:
index.php
<script language="javascript" src="validator.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
<form onSubmit="return checkForm();" method="post" action="action.php">
<table width="700" cellpadding="2" cellspacing="0" align="center" border="0">
<tr>
<td valign="top" align="left">
<span class="label">Marca:</span>
</td>
<td align="left">
<select id="marca">
<option value="">selectati</option>
<option value="xxx">altceva</option>
</select>
<div class="error" id="marcaError">Required: Please enter marca<br></div>
</td>
</tr>
<tr>
<td valign="top" align="left">
<span class="label">Name:</span>
</td>
<td align="left">
<input type="text" value="" id="name"><br>
<div class="error" id="nameError">Required: Please enter your name<br></div>
</td>
</tr>
<tr>
<td valign="top" align="left">
<span class="label">Email:</span>
</td>
<td align="left">
<input type="text" value="" id="email"><br>
<div class="error" id="emailError">Required: Please enter your email address<br></div>
</td>
</tr>
<tr>
<td valign="top" align="left">
<span class="label">Comment:</span>
</td>
<td align="left">
<textarea type="text" value="" id="comment"></textarea><br>
<div class="error" id="commentError">Required: Please enter a comment<br></div>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
Cod:
style.css
input {
width: 200px;
font-family: Tahoma;
font-size: 8pt;
}
.label {
width:50px;
}
textarea {
width: 200px;
font-family: Tahoma;
font-size: 8pt;
}
body {
font-family: Tahoma;
font-size: 8pt;
}
.error {
font-family: Tahoma;
font-size: 8pt;
color: red;
display:none;
}
Dupa cate observati am adaugat eu in js linile pentru marca. Problema e ca imi afiseaza ca nu e completat dar nu se opreste ca sa ma lase completez, imi ruleaza action.php. Codul este luat de pe net. Eu am ajuns la concluzia ca se intampla treaba asta deoarece nu este input type ci select. Ce anume trebuie sa fac in js ca sa imi ia si <select...>?
Multumesc anticipat[/b] |
|
| Sus |
|
Quber
Data înscrierii: 27/Iun/2006
Mesaje: 752
Locație: localhost
|
| Trimis: Sâm Apr 19, 2008 2:15 pm Titlul subiectului: |
|
|
| pune la input <input type="submit" onClick="return checkForm();" /> mi se pare ca asha o a mearga..! |
|
| Sus |
|
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Sâm Apr 19, 2008 2:30 pm Titlul subiectului: |
|
|
am adaugat si la submit ce ai zis tu si tot nu mere :(
am facut un link ca sa vedeti mai bine :
http://www.tubultau.com/validare/
Daca va uitati cand selectati altceva din rubrica marci restul merge
Multumesc |
|
| Sus |
|
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Sâm Apr 19, 2008 2:54 pm Titlul subiectului: |
|
|
gata...se pare ca trebuia sa sterg document.getElementById("marca").select();
Multumesc. offtopic |
|
| Sus |
|
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Sâm Apr 19, 2008 5:11 pm Titlul subiectului: |
|
|
Mai am o problema acum daca sunteti amabili :
vreau sa fac sa imi apara divurile cu erori odata si nu pe rand ? ms[/code] |
|
| Sus |
|
unknown
Data înscrierii: 30/Aug/2005
Mesaje: 147
|
| Trimis: Sâm Apr 19, 2008 9:39 pm Titlul subiectului: |
|
|
pune mana si citeste niste tutoriale javascript
Cod:
validator.js
function checkForm() {
marca = document.getElementById("marca").value;
name = document.getElementById("name").value;
email = document.getElementById("email").value;
comment = document.getElementById("comment").value;
var value1 = "none";
var value2 = "none";
var value3 = "none";
var value4 = "none";
if (marca == "") {
hideAllErrors();
value1 = "inline";
document.getElementById("marca").select();
document.getElementById("marca").focus();
} if (name == "") {
hideAllErrors();
value2 = "inline";
document.getElementById("name").select();
document.getElementById("name").focus();
} if (email == "") {
hideAllErrors();
value3 = "inline";
document.getElementById("email").select();
document.getElementById("email").focus();
} if (comment == "") {
hideAllErrors();
value4 = "inline";
document.getElementById("comment").select();
document.getElementById("comment").focus();
}
hideAllErrors(value1,value2,value3,value4);
return true;
}
function hideAllErrors(value1,value2,value3,value4) {
document.getElementById("marcaError").style.display = value1;
document.getElementById("nameError").style.display = value2;
document.getElementById("emailError").style.display = value3;
document.getElementById("commentError").style.display = value1;
} |
|
| Sus |
|
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Sâm Apr 19, 2008 11:06 pm Titlul subiectului: |
|
|
| ms frumos. |
|
| Sus |
|
vectorialpx
Data înscrierii: 01/Mar/2005
Mesaje: 2764
Locație: Targoviste City
|
| Trimis: Dum Apr 20, 2008 5:20 pm Titlul subiectului: |
|
|
| e indicat ca verificarile javascript sa fie facute doar 'de frumusete'... trebuie facuta verificare in limbajul de programare, avand in vedere ca js poate fi disabled ;) |
|
| Sus |
|
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Lun Apr 21, 2008 7:32 am Titlul subiectului: |
|
|
| si nu se pot face amandoua? :D adica din programare dar sa fie si frumos? |
|
| Sus |
|
mihaitha
Data înscrierii: 04/Mai/2007
Mesaje: 1166
Locație: Sibiu
|
| Trimis: Lun Apr 21, 2008 8:26 am Titlul subiectului: |
|
|
Normal ca se pot face amandoua. Tehnica se numeste graceful degradation.
Practic faci acelasi lucru de doua ori: verificare din js, astfel incat utilizatorul ce intra cu un browser js-enabled sa aiba formularul verificat fara sa dea refresh la pagina, si a doua verificare, aceeasi, dar din limbajul server-side (php I presume), pentru browserele non-js, care in caz de eroare se intoarce in pagina cu formularul, acesta fiind completat cu datele introduse. Easy as pie. |
|
| Sus |
|
radu_paraleste
Data înscrierii: 02/Iul/2007
Mesaje: 207
Locație: Galati
|
| Trimis: Lun Apr 21, 2008 8:41 am Titlul subiectului: |
|
|
| ioi...nustiam...eu facem cu sessiuni :D dar se pare ca lucram prost. o sa studiez mai multe. multumesc frumos |
|
| Sus |
|
PHPRomania Bot
Bot Member
Data înscrierii: 27/Dec/2007
Mesaje: 1
Locaţie: Server Google |
| Trimis: Mie Dec 26, 2007 7:01 pm Titlul subiectului: Ad |
|
|
|
|
|
| Sus |
|
| |