javascript expressions names [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 am a complete beginner to javascript and I have several things I need to correct on a form in order for it to work. I have to make sure it doesn't reject any valid names (names with accents, hyphens, names with spaces between them). At the moment my regular expression is -
var alphabetic = /^[a-zA-Z]+$/;
if ((alphabetic.test(fname)== false) || (alphabetic.test(lname)== false))
{
alertmsg = alertmsg + "Name should be in alphabets:" + "\n";
}
If someone could point me in the right direction, I would be very grateful

Try this regex :
var alphabetic = /^[a-zàâçéèêëîïôûùüÿñ-\s]+$/i

As Philippe recommended, if you would like to accept languages/alphabets other than English, I would consider more carefully which letters to include. [a-zA-Z] does not seem to recognize letters other than strictly 'A' to 'Z' in my testing.

Related

How to remove a group of characters from a phone string in javascript? [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 3 years ago.
Improve this question
I am willing to know how can I check if the a phone number contain the following prefixes +44, 0044 or 0 and if so it will be removed? I know I to remove a number of characters or a substring but how do I check if that substring is in the beginning?
Thanks in advance
You could with regex /^(\+44|0044|0)/g
function rem(str){
return str.replace(/^(\+44|0044|0)/g,'')
}
console.log(rem('0044987987'));
console.log(rem('04478687'));
console.log(rem('+447783'));
You can use indexOf to check the index of those prefix.
"+44-----".indexOf("+44") // returns 0
That should be enough for what you want.
You can simply test against a regex pattern.
Or use it for a replace.
const re_tel44 = /^[+]?0{0,2}(?:44)?(\d{5,})$/;
let phonenumber = '+4412345';
console.log(re_tel44.test(phonenumber));
if(re_tel44.test(phonenumber))
phonenumber = phonenumber.replace(re_tel44, '$1');
console.log(phonenumber);

Minimum characters with Wildcard character [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
I have the below wild card search which looks for the following conditions.
$.validator.addMethod("FirstName", function (value, element) {
return this.optional(element) || /^[a-zA-Z''-'\s]{3,20}[\%\*]{0,1}$/i.test(value);
}, "For wild card search minimum of 3 characters should be entered.");
$("#frmJscsSearch").validate({
rules : {
FirstName : "required FirstName"
},
});
if user enter 2 characters, its valid
If user enter 3 characters, its valid
How do I validate if user enter 2 characters WITH * then its invalid?
See if this regex does what you want (the idea is to use alternation |):
/(^[a-z '\-]+$)|(^[a-z '-]{3,}[\%\*]$)/i
Your original regex was a little messy also, so I've edited it.
You can also try checking for the wrong input instead of the right one:
var re = /([^a-z '\-*%])|(^[\w\W]{0,2}[\*\%]&)|(^[ \-*%'])|([ \-]$)|([ \-']{2,})/i
!(re.test(value))
That way you can check for more specific conditions easier. The regexp above describes the following rules:
value should contain only a-z '-*% characters;
* or % should be the last symbol (if used) preceded by at least three other characters;
value can't start with -'*%;
value can't end with -;
value can't contain multiple -' in a row.

Regx for correct filename 1201_17-11-2015.zip [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
I need to check whether a given file name is in the correct format or not.
Means:
first four numbers_two numbers-two numbers-4 numbers.zip
for that I need a regular expression.
Example file name is (1201_17-11-2015.zip) in javascript
var re = new RegExp('^\d{4}_\d{2}-\d{2}-\d{4}.zip$');
if (filename.match(re)) {
//successful match
}
You regexp could look something like this:
^\d{4}_\d\d-\d\d-\d{4}.zip$
^ is the beginning of your pattern
\d means any number
{n} means that the last pattern has to exist n-time
$ is the end of your pattern
On this site you can start learning how to use regular expression
Here you can test if your own regular expression is working...
I find it best to test scenarios at RegExr. None the less, what you're asking for is basic:
var result = "1201_17-11-2015.zip".match(/\d{4}_\d{2}-\d{2}-\d{4}\.zip/)
if (result == null) {
console.warn("Unable to find a match");
} else {
console.log("Found match: %k", result);
}

Regex pattern Alphanumeric and dollar sign [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
I would like to know how I set a regex pattern for aphanumeric and dollar sign.
Except for dollar sign, it does not accept any other special characters.
Here are examples...
The pattern should be okay with ....
hahah
hohho
hihihi
$hahah
hahah I will get $100 for this
The pattern should be sad with ....
hi James.
#fdasfdas
run!
Any idea?
so you want it to require a '$' symbol somewhere in the string? – yes.
Do you want to allow spaces also? - yes
please add more details, unless the below answer is what you are looking for. Currently this isn't a clear question. – sorry I just got back to my machine.
public static bool IsAlphanumericCharactersAndDollarSign(string str)
{
if (str == null) return false;
Regex rg = new Regex(#"/[a-zA-z0-9\s\$]*/");
return rg.IsMatch(str);
}
Pattern for this: /[a-zA-z0-9\s\$]*/ match alphanumeric, spaces and $ sign 0 or more times
This is PCRE compliant, but in perl for example you need to escape the $ with \$

regular expression with limited special characters in jquery [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 9 years ago.
Improve this question
I would like to all alphanumeric data +
only these following 4 special character are allowed.
' (single quote)
- (hyphen)
. (dot)
single space
I tried this :
var userinput = $(this).val();
var pattern = [A-Za-z0-9_~\-!##\$%\^&\*\(\)]+$
if(!pattern.test(userinput))
{
alert('not a valid');
}​
but it is not working.
First, you need to enclose the string in / to have it interpreted as a regex:
var pattern = /[A-Za-z0-9_~\-!##\$%\^&\*\(\)]+$/;
Then, you have to remove some unallowed characters (that regex is matching more than you specified):
var pattern = /^[A-Za-z0-9 '.-]+$/;
The second one is what you need. Complete code:
var userinput = $(this).val();
var pattern = /^[A-Za-z0-9 '.-]+$/;
if(!pattern.test(userinput))
{
alert('not a valid');
}​
Besides, check what this points to.
"Not working" is not a helpful description of your problem.
I'd suggest this regular expresion:
^[a-zA-Z0-9\'\-\. ]+$

Categories