JavaScript Tutorials
- What is Javascript?
- What is BOM?
- What is DOM?
- Variable
- check variable value is empty
- JavaScript Output
- Functions
- Javascript Events
- Input Events
- onchange Event
- Javascript Output methods
- If else statement
- Arrays
- Pattern Validation
- Form Validation
- Inner Html Form Validation
- Inline Form Validation
- Checkbox Validation
- Inline Inner Html Form Validation
- Server side php Validation
- Validate a HTML Form with PHP
- window and window location
- Get Text Value
- Get hidden field value
- JavaScript & PHP
- Date Format
- get php value in javascript
- Redirect page & Autoredirect page
- Auto Refresh page & Div
- How to get select text value from Dropdown box
- How to clear browser history in javascript
- Checkbox Problems
- Select option problems
- Popup Contact Form
- Sidebar Contact Form
- How to use a multistep Form or Form wizard
- Auto Calculate Price
- print Application Form
- Auto Calculate GST in Javascript by select price
- Calculate GST by input value in text box Jquery
- Calculate Discount
- onClick Checkbox
- autofil form data click on checkbox
- Show subcategory list
- Show city list as per state
- Show district list as per country and state
- Show good morning good night wish
- image upload with preview image
- Print Div Content
- Show modal popup on page load
- filter table data usign javascript.
- Character Limit Validation.
- Validate File 5MB Upload
- Validate Special character
- More File Upload
- Call JavaScript Function After Page Load
- Drop Down First option Value Disabled --- Please Select ---
- How to Disable Submit Button After Form Submission
- How to disable browser back button using Jquery?
- How to Remove selected item from second & third dropdown list?
- Interview Questions of JavaScript.
Important Link
How to validate Special Character in javascript?
File name : index.php
<input type="text" class="form-control" name="address" id="address" value="<?php echo set_value('address'); ?>" placeholder=" Enter Address"
onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;"
/>
<span id="error" style="color: Red; display: none">* Special Characters not allowed.</span>
<span class="required" id="address_errr" aria-required="true"></span>
<span class="error"><?php echo form_error('address'); ?></span>
File name : index.php
<script type="text/javascript">
// special characters not allowed.
var specialKeys = new Array();
specialKeys.push(8); //Backspace
specialKeys.push(9); //Tab
specialKeys.push(46); //Delete
specialKeys.push(36); //Home
specialKeys.push(35); //End
specialKeys.push(37); //Left
specialKeys.push(39); //Right
function IsAlphaNumeric(e) {
/* var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 &&charCode != e.keyCode));
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
*/
var k;
document.all ? k = e.keyCode : k = e.which;
var ret = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
}
</script>
Example 2: How to validate special characters in text box using javascript?
File name : index.php
<input type="text" class="form-control" name="address" id="address" value="<?php echo set_value('address'); ?>" placeholder=" Enter Address"
onkeyup="Validate_specialChar(this.id)" />
<span id="error" style="color: Red; display: none">* Special Characters not allowed.</span>
<span class="required" id="address_errr" aria-required="true"></span>
<span class="error"><?php echo form_error('address'); ?></span>
<textarea class="form-control requireds" name="subject" id="subject" placeholder=" Enter Subject" rows="3" cols="50" onkeyup="ValidatespecialChar(this.id)" maxlength="500" ><?php echo set_value('subject'); ?></textarea>
<span class="error"><?php echo form_error('subject'); ?></span>
<span class="required" aria-required="true" style="color:green;float: right;">Maximum 500 characters are allowed</span></br>
<span class="required" id="subject_errr" aria-required="true"
<script>
function Validate_specialChar(id){
var yourInput = $('#'+id).val();
re = /[`"~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);
if(isSplChar){
var no_spl_char = yourInput.replace(/[`"~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
$('#'+id+'_errr').text('Special Character not allowed');
}
}
function ValidatespecialChar(id){
var maxLength = 500;
var yourInput = $('#'+id).val();
re = /[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);
if(isSplChar){
var no_spl_char = yourInput.replace(/[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
$('#'+id+'_errr').text('Special Character not allowed');
}
var textlen = maxLength - $('#'+id).val().length;
$('#'+id+'_errr').text(textlen+' Character remaining');
}
</script>
<script type="text/javascript">
function ValidatespecialChar(id){
var yourInput = $('#'+id).val();
re = /[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);
if(isSplChar){
var no_spl_char = yourInput.replace(/[`~!@#$%^&*()|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
}
}
</script>
function ValidatespecialChar(id){
var yourInput = $('#'+id).val();
re = /[`~!@#$%^&*()?|+\<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(yourInput);
if(isSplChar){
var no_spl_char = yourInput.replace(/[`~!@#$%^&*()|+\<>\{\}\[\]\\\/]/gi, '');
$('#'+id).val(no_spl_char);
}
}
</script>