Po wciśnięciu przycisku (button) skrypt działa właściwie, natomiast gdy button zamienię na submit, po wciśnięciu nic się nie dzieje.
Próbowałem ruszyć akcję za pomocą <a href>, jednak również nie działa właściwie.
Funkcja ma za zadanie sprawdzić, czy wpisana wartość zawiera daną frazę a także czy nie zawiera zabronionych fraz.
Czy mogę liczyć na pomoc?
Jeśli ktoś był by w stanie poprawić mój kod, byłbym bardzo wdzięczny. Prosze o wyrozumiałość, gdyż w JS bawię sie dopiero kilka dni:)
Kod
<script type="text/javascript" language="javascript">
function validate(){
var inputs = (document.getElementsByTagName('input').length)-1;
var fields = new Array;
for (var i = 0; i < inputs; i++)
{
id = "input[" + i +"]";
fields[i] = document.getElementById(id).value;
}
//Create Variable to Keep Track of Errors
var err = 0;
//Start Validation Loop
for (i=0;i<fields.length;i++){
var x=fields[i];
//////////////////////////////////////////////////////
//Frazy dozwolone //
var accpos = new Array; //
accpos[0] = x.indexOf("embed"); //
//
//Frazy niedozwolone //
var notpos = new Array; //
notpos[0] = x.indexOf("file"); //
notpos[1] = x.indexOf("iframe"); //
//////////////////////////////////////////////////////
//Check Fields in Array to Make Sure they are not Empty
if (fields[i] == "" || accpos[0] < 0 || notpos[0] >= 0 || notpos[1] >= 0){
id = "showme[" + i +"]";
document.getElementById(id).style.display="block";
err++;
}
}//Close Loop
//Check That There are No Errors
if (err === 0){
//Submit Form
document.myform.submit();
}else {
//If there are errors, return false and alert the user
return false;
}
}
</script>
function validate(){
var inputs = (document.getElementsByTagName('input').length)-1;
var fields = new Array;
for (var i = 0; i < inputs; i++)
{
id = "input[" + i +"]";
fields[i] = document.getElementById(id).value;
}
//Create Variable to Keep Track of Errors
var err = 0;
//Start Validation Loop
for (i=0;i<fields.length;i++){
var x=fields[i];
//////////////////////////////////////////////////////
//Frazy dozwolone //
var accpos = new Array; //
accpos[0] = x.indexOf("embed"); //
//
//Frazy niedozwolone //
var notpos = new Array; //
notpos[0] = x.indexOf("file"); //
notpos[1] = x.indexOf("iframe"); //
//////////////////////////////////////////////////////
//Check Fields in Array to Make Sure they are not Empty
if (fields[i] == "" || accpos[0] < 0 || notpos[0] >= 0 || notpos[1] >= 0){
id = "showme[" + i +"]";
document.getElementById(id).style.display="block";
err++;
}
}//Close Loop
//Check That There are No Errors
if (err === 0){
//Submit Form
document.myform.submit();
}else {
//If there are errors, return false and alert the user
return false;
}
}
</script>
Kod
<form method='post' name='myform' action=''>
<?php
for($x = 0; $x < 3; $x++)
{
echo"
array: <input type='text' id='input[$x]' name='input[$x]'/><br />
<div id='showme[$x]' style='color: red; display: none;'>Wprowadzony link jest nieprawidłowy.</div>
";
}
echo $_POST['input'][0];
?>
<input type='button' value='Log Me In' onClick='validate();'/>
</form>
<?php
for($x = 0; $x < 3; $x++)
{
echo"
array: <input type='text' id='input[$x]' name='input[$x]'/><br />
<div id='showme[$x]' style='color: red; display: none;'>Wprowadzony link jest nieprawidłowy.</div>
";
}
echo $_POST['input'][0];
?>
<input type='button' value='Log Me In' onClick='validate();'/>
</form>