Require hyphens using javascript regex [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I keep seeing regex on phone numbers that allows the number as all one string, and does not make someone enter the hyphen. Using these:
/^[-\s]$/
/^(-\s)$/
/^[-]$/
/^(-)$/
/^([-]\s)$/
as a regex allows spaces or no hyphen to be typed at all. How do you require a hyphen to be inserted?
EDIT: There shouldn't be downvotes that claim "you shouldn't be forcing someone to use this!". This is the client's requirement, and if you ever had to read phone numbers that were 1234567890 instead of 123-456-7890, that would enable you to see an area code and phone exchange at a glance, I would think anyone would want this.
And not that it should matter to the question in any way because it was kept vague and specific on purpose, but this is for a textbox that will not require any non-NANP numbers. I did that on purpose so we can focus on how to require hyphens, not reinvent the wheel on phone number regexs.

This simple regex will do the job:
^\d{3}-\d{3}-\d{4}$

Related

JavaScript RegExp to match a given string only if it is not surrounded by brackets [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am looking for a JavaScript regular expression that would match the word "foo" in a phrase "This <is foo" or "This is foo" or "This is foo>" but would not match in the phrase "This <is foo>". I know that in other languages this is solved by using negative lookbehinds, but AFAIK, JavaScript does not support these. I have come across similar questions, but none of them matched exactly what I am looking for, and I can't think of a solution on my own. I would be grateful for any hint.
I am testing my attempts here.
You can match it as group not bounded by the opening and closing brackets.
[^<].*(foo).*[^>]

Differentiate full name without spaces [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
If i have Full name coming as an input from the user without any spaces, how would i be able to differentiate first name and last name. The Full name for the input could be any name (generic input).
In the general case, you can't.
There are some things you might be able to do depending on what you receive. For instance, if you get "JohnSmith", then you can probably split them on the first uppercase character that isn't the first character of the string, but that could break in all kinds of ways —"ManueldelaVega", for instance, or "JOHNSMITH" or "mohammedibnabdul".
So again: In the general case, you can't.

how to include both digits with hyphens in javascript pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
how to include both digits and hyphens in javascript pattern. Like in phone number where user can add both digits and hyphens. No matter where is the hyphen.
The regular expression to make this is so simple. You can make something like this:
([0-9\-]+)
That means all numbers and hypen no matter where is. You can test it here:
https://regex101.com/r/eX3yA7/1

filter invalid mobile numbers with Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to write in java script to exclude invalid mobile numbers that are entered into the SQL database. i currently have the regex function within my script however it cant pick up brackets(), resulting in numbers such as (123) 456789 not being included.
Is there any extension to the regex i could use to include brackets?
There is a free google API that can validate numbers for you, even tell you the country code etc.
https://code.google.com/p/libphonenumber/
Never tried it, but I evaluated it once for another project I worked on.
This is Java, not JS, but still you may consider moving your validation logic to a Java servlet and invoke using an AJAX call.
1) This would expect atleast one white space character after parenthesis
/\(\d{3}\)\s+\d{6}/
Or
/\(\d+\)\s+\d+/ //in case of digit not specified.
2) This would match with or without space
/\(\d{3}\)\s*\d{6}/
//eg:-
// (123)456789
// (123) 456789

Where are javascript's regular expressions (regex) used? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Are they useful to know? How are they used?
Regular expressions are not a JavaScript-specific feature and are typically used to check for faults in use input, for example to notify users who forget the # or domain part of an email address or too few/many digits in a phone number. For more examples, look over there.
Email address validation
JavaScript is like Perl, has native regular expression notation.
Regular expressions are used to match texts. For example, validating age:
if (field.value.match(/^\s*\d+\s*$/) === null) alert("error: invalid age.");
Read this for a documentation: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
and JS form validation tutorial:
http://www.devarticles.com/c/a/JavaScript/Form-Validation-with-JavaScript-Regular-Expressions-Part-1/

Categories

Resources