I have phone area textbox and i want to test it against values like 1111111 or 9999999.
I want at least 1 character to be different in this text area(it is exactly 7 characters long)
How can i do this in its shortest way?
I have phone area textbox and i want to test it against values like 1111111 or 9999999.
I want at least 1 character to be different in this text area(it is exactly 7 characters long)
How can i do this in its shortest way?
Short answer: the goal is to reject strings of length seven where every character is the same (for example "1111111"). is right to look at repetition patterns; there are two tidy ways to enforce this depending on whether you prefer a single regex or a clear, easy-to-read check.
A simple, readable JavaScript approach (no complex backreferences):
function isInvalidPhone(s) {
s = (s || "").trim();
if (s.length !== 7) return true; // not seven characters => invalid
return s === s[0].repeat(7); // invalid when all seven chars are identical
} A compact single-regex solution that accepts exactly seven digits but rejects seven identical digits:
// true when valid: exactly seven digits and not all the same
var valid = /^(?!([0-9])\1{6})[0-9]{7}$/.test(input); Notes: always trim and normalize the input first, and decide whether separators or parentheses are allowed (strip them before validating). String.prototype.repeat is ES6; if you must support very old engines, replace it with a short loop. Do server-side validation as well, and favour the clearer approach in codebases where readability and maintainability matter; use the single-regex option when you need compact, one-step validation.
You should still make your checks for length before this but this will check for numbers like 1111111 and 999999
var phone_number = "9999999";
if (!/^(\d)\1{6}$/.test(phone_number)) {
alert('Good phone number');
} else {
alert('Bad phone number');
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.