saarsc saarsc - 7 months ago 9
Javascript Question

How to check if a string contain a list if characters which are not in english

I want to valid textbox text. the textbox is for a phone number so, of course, letters and anything other the number I used different kinds of code

ex

$(document).ready(function () {
$("#sub").click(function () {
var regex = /[$%^&*()_+[]{}<>?]אבגדהוזחטיכךלמםנןסעפצקרשת \"/
var bleangth = $('.bphone').val().length;
var eleangth = $('.ephone').val().leangth;
var cbphone = $('.bphone').val();
var cephone = $('.ephone').val();
if (bleangth < 3 || eleangth < 7) {
$('#bad').removeClass("error");
$('#short').addClass("error");

}
else
if (regex.test(cbphone) || regex.test(cephone)) {
$('#short').removeClass("error");
$('#bad').addClass("error");
}
else {
$('#bad').removeClass("error");
$('#short').removeClass("error");
//$('h3').removeClass("snerror")
}
alert( "end " + regex.test(cephone) +" start " + regex.test(cbphone));
});


As you see I entered letters from different language but for some reason the code can't "read" it and return true is there a way maybe to get string split it to array and then use it? or different code or something I need to add/ download

Thank you in advance

Answer

If requirement is for only numbers to be input you should be able to use NOT operator ! operator with RegExp /\D/ ; e.g.; !/\D/.test(/*input*/) where true would be result if input is a digit, false if input is not a digit.


An approach using HTML5 <input> element with pattern attribute set to \d+, required attribute; <label> element, CSS :focus, :invalid , adjacent siblings selector +

input + label {
  display:none;
}

input:focus:invalid + label {
  display:block;
  color:red;
}
<input type="text" 
       pattern="\d+"
       placeholder="Please input digits 0-9"
       required /><label>Invalid input. Please input digits 0-9</label>

Comments