Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

javascript - Validation for accepting numbers and special characters using jQuery

I have written a validation for accepting numbers and special characters but it is accepting only numbers while validating not accepting special characters.

$(document).ready(function () {
$("#law_degree_academic_years").keypress(function (e) {
 if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
   $("#errmsg").html("Digits Only").show().fadeOut("slow");
           return false;
}
 });
});

<div class="form-group"> 
                                    <label for="law_degree_academic_years" class="col-sm-2 control-label">41. Law Degree Academic Years</label> 
                                    <div class="col-sm-9"> 
                                        <input type="text" class="form-control" value="<?php echo $oppointArr['law_degree_academic_years'];?>" name="law_degree_academic_years" id="law_degree_academic_years" placeholder="Ex:2017-2020" minlegth="9" maxlength="9" required> 
                                    </div> 
                                </div> 
question from:https://stackoverflow.com/questions/65841154/validation-for-accepting-numbers-and-special-characters-using-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this method

function Validate() {
        var isValid = false;
        var regex = /^[0-9-+()]*$/;
        isValid = regex.test(document.getElementById("TextBox1").value);
        return isValid;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...