Merge pull request #8 from esensar/AJAX

Country code validation
pull/9/head
esensar 2016-05-22 18:57:29 +02:00
commit daae4ba6d0
5 changed files with 54 additions and 2 deletions

10
add.php
View File

@ -8,11 +8,19 @@
Slika link:
</label><br />
<input type="text" id="emailField" name="slika"/> <br />
<label >
Country code:
</label><br />
<input type="text" id="ccode" name="countryCode" onblur="validateCountry(this)"/> <br />
<label >
Broj telefona:
</label><br />
<input type="text" id="brtel" name="phoneNumber" onblur="validatePhone(this)"/> <br />
<label >
Tekst:
</label><br />
<textarea type="text" rows="30" cols="70" id="passwordField" name="vijest" ></textarea><br />
<input type="submit" name="submit" value="Submit" />
<input id="input" type="submit" disabled="true" name="submit" value="Submit" />
</form>
</div>
</BODY>

View File

@ -27,7 +27,6 @@
}
}
print_r($slike);
$time = date(DATE_ISO8601, strtotime("now"));
$vijestiFile = $time.','.$slike.','.$vijest."\n".$vijestiFile;
$vijestiFile = strip_tags($vijestiFile);

View File

@ -84,6 +84,12 @@
<option value="monthly">Novosti ovaj mjesec</option>
</select>
</li>
<li class="headerItem">
<select id="sort" name="sort" onchange="onSort(this.value)">>
<option value="date">Sortiraj po datumu</option>
<option value="alpha">Sortiraj abecedno</option>
</select>
</li>
<?php endif; ?>
</ul>
</div>

37
js.js
View File

@ -89,6 +89,43 @@ function onFilter(select) {
}
}
function validateCountry(countryCodeField) {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
var nekiResponseObjekatNesto = JSON.parse(ajax.response);
var nizko = [];
for(var i = 0; i < nekiResponseObjekatNesto.length; i++) {
nizko.push(nekiResponseObjekatNesto[i].callingCodes);
}
document.getElementById("brtel").data = nizko;
validatePhone(document.getElementById("brtel"));
} else if (ajax.readyState == 4) {
document.getElementById("brtel").style.backgroundColor = "red";
document.getElementById("input").disabled = true;
}
}
ajax.open("GET", "https://restcountries.eu/rest/v1/alpha?codes=" + countryCodeField.value, true);
ajax.send();
}
function validatePhone(phoneField) {
var nizko = phoneField.data;
console.log(nizko);
if(!nizko) return;
for(var i = 0; i < nizko.length; i++) {
console.log(phoneField.value.indexOf(nizko[i]));
var index = phoneField.value.indexOf(nizko[i]);
if(index == 0 || index == 1) {
phoneField.style.backgroundColor = "white";
document.getElementById("input").disabled = false;
return;
}
}
phoneField.style.backgroundColor = "red";
document.getElementById("input").disabled = true;
}
function validateEmail(emailField) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(re.test(emailField.value)) {

View File

@ -0,0 +1,2 @@
2016-05-22T18:53:53+0200,,hljebovi
2016-05-22T18:52:02+0200,+38761553000,Kemicin telefon se nigdje u bazi ne spasava
1 2016-05-22T18:53:53+0200 hljebovi
1 2016-05-22T18:53:53+0200 hljebovi
2 2016-05-22T18:52:02+0200 +38761553000 Kemicin telefon se nigdje u bazi ne spasava