Javascript Course

In this tutorial is presented a way to check /validate on the client side (with JavaScript) the values added in text fields in a form, the allowed characters (specified with RegExp) and their number.

Here it is presented a simple example, with 3 text fields. Takes the value from each text box, and then check one by one each box.
In the same way you can apply to forms with multiple text boxes and textarea. See the explanation below, and the comments in the code.


Script code with example

<i>- Name and Password must contain only numbers, letters and dashes '-', '_'.<br>
- Name between 3 and 18 characters, Password between 7 and 18 characters.</i>
<form action='#' method='post' onsubmit='return checkForm(this);'>
 <label for='name1'>Name:</label> <input type='text' name='name1' id='name1' /><br>
 <label for='pass1'>Password:</label> <input type='password' name='pass1' id='pass1' /><br>
 <label for='email1'>E-mail:</label> <input type='text' name='email1' id='email1' /><br>
 <input type='submit' name='fsubmit' id='fsubmit' value='Submit' />
</form>
<script>
// RegExp to allow only numbers, letters and dashes '-', '_'
var regx_chr = /^([a-zA-Z0-9_-]+)$/;
var regx_mail = /^([a-zA-Z0-9]+[a-zA-Z0-9._%-]*@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4})$/; // RegExp for e-mail address

// Function accessed by 'onsubmit'
// gets the form fields and check /validate their value
function checkForm(frm1) {
 var fre = false; // variabile returned by this function

 // gets each text box
 var name1 = frm1.name1;
 var pass1 = frm1.pass1;
 var email1 = frm1.email1;

 // check the values (Name, Password, E-mail). If they are incorrect, it returns an alert, then selects that field
 if(name1.value.length<3 || name1.value.length>18 || name1.value.search(regx_chr)==-1) {
 alert('The Name must contain between 3 and 18 characters \nNumbers, Letters, - and _');
 name1.select(); // select the field for Name
 }
 else if(pass1.value.length<7 || pass1.value.length>18 || pass1.value.search(regx_chr)==-1) {
 alert('The Password must contain between 7 and 18 characters \nNumbers, Letters, - and _');
 pass1.select(); // select the field for Password
 }
 else if(email1.value.search(regx_mail)==-1) {
 alert('Add a correct e-mail address');
 email1.select(); // select the field for E-mail
 }
 else fre = true;

 return fre;
}
</script>
When the Submit button is clicked (or Enter on a field) to submit the form, the event 'onsubmit' is triggered. The code: 'return checkForm(this);' calls and returns the result of the 'checkForm()' function.
- 'this' represents the current object, the form.
The function returns the value of a variable (fre) initially set false.
The parameter 'frm1' stores the form, passed with 'this'.
The code 'frm1.field_name' takes the field ('field_name' is the name of the field, added in the 'name' attribute).
By adding the 'value' keyword, it takes its value, then, with 'length' gets the number of characters.
If the values ​​do not correspond with the data to be added, an alert displays a message, the 'fre' variable remains false, which determines not submitting the form.
If the values ​​are correct, the last instruction: 'else fre = true;' , sets the 'fre' variable true, that determines submitting the form.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Check and Validate characters in form fields

Last accessed pages

  1. PHP Simple HTML DOM Parser (12452)
  2. Disable button and Enable it after specified time (17532)
  3. Get Mime Type of file or string content in PHP (6229)
  4. PHP MySQL - using MySQLi (9669)
  5. Integer and Float value in Select with PDO from string to numeric (8658)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)