Hi to all, Can you please suggest me, How a Javascript validation can be applied in my form so that a when a user enters wrong data in the form, a Error message should alert at a time " Before (click on submit button or) submitting the form " ? (1) "Invalid" or "Numeric value" should Not be entered in the textbox of "Name" field . (2) when a user enters more than two digits in the textbox of "Age" field, at a time a message should alert for Not to enter more than two digit values. (3) when a user enters more or less than 10 digits for his mobile number, an Error messsage should alert at a time that you entered more or less than 10 digit numbers in the textbox of "Mobile" field. please enter only 10 digit number. please suggest me javascript validation for throwing Error message alert at a time of mistake , before submitting the form. In advance , Thanks & Regards.
Take a look at my post on another thread. You need to give a id="" to each input and add onsubmit="return validate()" to the <form> tag (1) if ( document.getElementById('name').value == '') { //id="name" error = error+"\n- Name must not be empty."; } else { if ( !isNaN(document.getElementById('name').value)) { error = error+"\n- Name must not be numeric."; } } HTML: (2) var age_value = document.getElementById('age').value;// id="age" if ( age_value=='') { error = error+"\n- Age must not be empty."; } else { if ( isNaN(age_value)) { error = error+"\n- Age must be numeric."; } else { if ( age_value > 99 ) { error = error+"\n- Age must be numeric & cannot be more than 2 digits."; } } } HTML: (2) var mobile_value = document.getElementById('mobile').value;// id="mobile" if ( mobile_value=='') { error = error+"\n- Mobile Number must not be empty."; } else { if ( isNaN(mobile_value)) { error = error+"\n- Mobile Number must be numeric."; } else { if ( mobile_value.length != 10 ) { error = error+"\n- Mobile Number must be 10 digits."; } } } HTML: Final codes. <script type="text/javascript"> function validate() { var error=''; // an empty error string if ( document.getElementById('name').value == '') { //id="name" error = error+"\n- Name must not be empty."; } else { if ( !isNaN(document.getElementById('name').value)) { error = error+"\n- Name must not be numeric."; } } var age_value = document.getElementById('age').value;// id="age" if ( age_value=='') { error = error+"\n- Age must not be empty."; } else { if ( isNaN(age_value)) { error = error+"\n- Age must be numeric."; } else { if ( age_value > 99 ) { error = error+"\n- Age must be numeric & cannot be more than 2 digits."; } } } var mobile_value = document.getElementById('mobile').value;// id="mobile" if ( mobile_value=='') { error = error+"\n- Mobile Number must not be empty."; } else { if ( isNaN(mobile_value)) { error = error+"\n- Mobile Number must be numeric."; } else { if ( mobile_value.length != 10 ) { error = error+"\n- Mobile Number must be 10 digits."; } } } if (error != '') { // error string is not empty alert('Please correct the following error to continue:'+error) return false; } else { return true; } } </script> HTML:
Hello sir, great thanks for your precious suggession. The js validation described by you is excellent & it is also working correctly for my condition also , But the problem is that it alert the error message after the (click of submit button) or submit operation is performed. Is there any way that it can alert the Error message instantly at the time of mistake of wrong entry in the form is done by the user , before the form (submit button is clicked ) submission is performed. Thanks & Regards.
try this Put this between head tags. <script type="text/javascript"> <!-- function validate_form ( ) { valid = true; if ( document.contact_form.contact_name.value == "" ) { alert ( "Please fill in the 'Name' box." ); valid = false; } return valid; } //--> </script> PHP: this is the code you should use on your html or php <form id="contact_name" name="contact_form" method="post" action="add.php" onSubmit="return validate_form ( );"> <input name="name" type="text" id="contact_name" size="40" /> <input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /> PHP:
Hello sir, Thanks for your precious support & effort. Please suggest me that Can I use the "onblur event" of javascript for my requirement as i want to show the Error message as the user leaves the field after entering the wrong value ? if yes what shoud be the right way or code that can be applied. Thanks & Regards.