Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
<form action="test.php" method="post">
<label for="exname">Name: <input type="text" id="exname" name="exname" /><span class="err"></span></label>
<div>Password: <input type="password" id="expass" name="expass" /><span class="err"></span></div>
<label for="exmail">E-mail: <input type="text" id="exmail" name="exmail" /><span class="err"></span></label><br/>
<input type="submit" id="exsbm" disabled="disabled" value="Send" />
</form>
<script>
// ID of the Submit button, and an Array with the IDs of the fields which to validate
var checkFormElm = function(id_sbm, fields) {
var phpcheck = 'check.php'; // Here add the php file that validate the form element
var elm_sbm = document.getElementById(id_sbm); // the submit button
var fields = fields || []; // store the fields ID
var elm_v = {}; // store items with "elm_name:value" (to know when it is changed)
var err = {}; // stores form elements name, with value of -1 for invalid, value 1 for valid, and 0 for not checked
// change the css class of elm
var setelm = function(elm, val) {
// if val not empty, sets in err an item with element name, and value of 1
// sets border to this form element,
// else, sets in err an item with element name, and value of 0, and removes the border
if(val != '') {
err[elm.name] = -1;
elm.className = 'errinp';
if(elm_sbm) elm_sbm.setAttribute('disabled', 'disabled'); // disables the submit
elm.parentNode.querySelector('.err').innerHTML = val; // adds the error message
}
else {
err[elm.name] = 1;
elm.className = 'vinp';
elm.parentNode.querySelector('.err').innerHTML = ''; // removes the error message
// check if invalid or not checked items in $err (with value not 1)
var inv = 0;
for(var key in err) {
if(err[key] != 1) {
inv = 1;
break;
}
}
// if not invalid element, enables the submit button, and sends the form
if(inv == 0 && elm_sbm) {
elm_sbm.removeAttribute('disabled');
elm_sbm.form.submit();
}
}
}
// sends data to a php file, via POST, and displays the received answer
var checkAjax = function(elm) {
if(elm.value != '' && (!elm_v[elm.name] || elm_v[elm.name] != elm.value)) {
elm_v[elm.name] = elm.value; // store name:value to know if was modified
var xmlHttp = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); // gets the XMLHttpRequest instance
// create pairs index=value with data that must be sent to server
var datatosend = elm.name +'='+ elm.value;
xmlHttp.open("POST", phpcheck, true); // set the request to php file
// adds a header to tell the PHP script to recognize the data as is sent via POST
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(datatosend); // calls the send() method with datas as parameter
// Check request status
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) setelm(elm, xmlHttp.responseText);
}
}
else if(elm.value =='') setelm(elm, 'This field must be completed.');
}
// register onchange event to form elements that must be validated with PHP via Ajax
for(var i=0; i<fields.length; i++) {
if(document.getElementById(fields[i])) {
var elm = document.getElementById(fields[i]);
err[elm.name] = 0; //store fields-name in $err with value 0 (not checked)
elm.addEventListener('change', function(e){ checkAjax(e.target);});
}
}
}
/* USAGE */
// array with IDs of the fields to validate
var fields = ['exname', 'expass', 'exmail'];
// create an object instance of checkFormElm(), passing the ID of the submit button, and the $fields
var chkF = new checkFormElm('exsbm', fields);
</script>
<style>
/* style for validated field */
.vinp {
background: #efeffe;
border: 1px solid blue;
}
/* style for invalided field */
.errinp {
background: #fefbda;
border: 2px dashed red;
}
/* style for error message */
.err {
margin: 0 5px;
color: red;
font-style: italic;
}
</style>