Javascript date pattern - javascript

I am trying to create JavaScript pattern to see if input value is date, my date format is 2015/Jan/01, I have tried doing this \d{4}/\[A-Za-z]{3}/\d{1,2} but that did not work, Please help me in fixing it.
function SetValue() {
// var newdata = document.getElementById('txtCellEditor').value;
var myString = txtCellEditor.value;
if (myString.match(\d{4}/\[A-Za-z]{3}/\d{1,2})) {
alert("'myString' is date.");
}

Your regex is incorrect
Use delimiters of regex i.e. / at the start and end of regex
Use ^ and $ to check exact match instead of substring
Escape the / by preceding them with \
No need to escape [
Use test() instead of match
Visualization
Demo
var regex = /^\d{4}\/[a-z]{3}\/\d{1,2}$/i;
function SetValue() {
var myString = document.getElementById('date').value;
if (regex.test(myString)) {
console.log("'myString' is date.");
} else {
console.log("'myString is not date.");
}
}
<input type="text" id="date" onkeyup="SetValue()" />

Most of the actual regular expression is fine, but you've forgotten to make it a regular expression by putting it in /s! (And you've escaped one of the / [but backward], but not the other.)
if (myString.match(/\d{4}\/[A-Za-z]{3}\/\d{1,2}/)) {
// ^ ^^ ^^ ^
// regex quote-/ \--escaped--/ \-- regex quote

Related

regular expression for numeric value; at most 3 decimal places

I'm trying to validate a form using regular expressions, the conditions are:
It has to be a numeric value
It CAN have up to three decimal places(0,1,2 are allowed too)
It has to be divided by a comma(,)
I already got it to work using HTML5-Patterns with this:
pattern='\d+(,\d{1,3})?'
Since patterns are not supported by IE9, I tried doing it with js:
var numPattern = /\d+(,\d{1,3})?/;
if(!numPattern.test(menge.val()))
{
checkvalidate = false;
}
Where did I go wrong?
Examples
valid: 1,234 ; 2,00 ; 5 ; 0,1
invalid: 1,2345 ; 2.00 ; 56a
You'll need to start your regex with ^ and end it with $ to make sure the entire input string/line is matched.
/^\d+(,\d{1,3})?$/
Here's a "demo" in which all your examples are valid/invalid:
https://regex101.com/r/oP5yJ4/1
(Using regex101.com to debug your regular expression patterns is often very useful)
Note that: (without ^ and $)
var pattern_without = /\d+(,\d{1,3})?/;
pattern_without.test("56a") === true; // matches, but only "56"
pattern_without.test("1,2345") === true; // matches, but only "1,234"
but: (with ^ and $)
var pattern_with = /^\d+(,\d{1,3})?$/;
pattern_with.test("56a") === false; // no match
pattern_with.test("1,2345") === false; // no match
You can use this regex:
/^\d+(?:,\d{1,3})*$/
RegEx Demo
Try this expression:
\d+(,\d{3})*([.]\d{1,3})?
Valid examples:
1,200.123
1,200.12
1,200.1
1.123
1,200,222
1,200,002
You can use the RegExp object.
var str = "123545,123";
var patt = new RegExp("/^(?:\d*\,\d{1,3}|\d+)$/");
var res = patt.test(str);
After execution, res will be true, since str matches the pattern you're looking for,

Javascript Regular Expressions are not working; always returns false

I need to write a regular expression that will check that the strings matches the format 'ACT' followed by 6 digits eg. 'ACT123456'
Though it looks quite simple, none of my options work; the function always returns false.
I tried the following combinations:
Pure regexpression literals
var format = /^ACT\d{6}$/;
var format = /^ACT[0-9]{6}$/;
Or using RegExp object with double escaping (eg. \\d) and with single escaping (\d)
var format = new RegExp("^ACT\\d{6}$");
var format = new RegExp("^ACT[0-9]{6}$");
My function for testing is:
function testPattern(field, pattern) {
if (!pattern.test(field)) {
return false;}
else {
return true;
}}
var format = /^ACT\d{6}$/;
works fine but the string must be ACT123456 exactly with nothing preceding it or following it
eg 'ACT123456 ' fails
use
/ACT\d{6}/
to allow more tolerance or strip the whitespace from the string first
var testString = "ACT123456"; // string to test
// pattern as a regex literal
var pattern = /^ACT[0-9]{6}$/g;
console.log(testString.match(pattern)); // output: ["ACT123456"]
Thanks you all guys for answers and feedback!
After being stuck with regular expressions, I realized that my problem with that I am not using field.value in my function.
So, the problem is with the function that must be:
function testPattern(field, pattern) {
if (!pattern.test(field.value)) {
return false;}
else {
return true;
}}

regex string correct but not working

I have a regex expression which is
function checkNum(form) {
var regex = "/^\d+/";
if (!form.value.match(regex)) {
alert('Please Enter value again');
form.value = "";
}
}
but it is not working.
I have entered this regex expression /^\d+$/ in this website http://www.regular-expressions.info/javascriptexample.html but it doesnt seem to work too in this website. I am pretty sure this expression is correct
Regex literal is /^\d+/ without quotes.
function checkNum(form) {
var regex = /^\d+$/; //Pure digits from start to end
if (!form.value.match(regex)) {
alert('Please Enter value again');
form.value = "";
}
}
Demo http://jsfiddle.net/KvuaM/
If you pass the string "/^\d+/" to .match() you will get a regex equivalent of /\/^d+\// because of the implicit conversion and invalid "\d" escape.
new RegExp("/^\d+/").source
//"/^d+/"
You have slashes in the string, which are interpreted as literals.
Either remove them from the string, or use them in order to create a regular expression
"^\d+"
or
/^\d+/

RexExp in javascript dont match a number inside a string

Im learning Regular Expresions in Javascript and there is a thing that i dont understand.
The following regexp should match any string from a to z but if I add a number it says that is correct
var patron = /[a-zA-Z]/;
var regex = new RegExp(patron);
var v= "hello word 512";
if(v.match(regex))
{
//should not match but it does
}else
{
objInput.style.color = "red";
}
And them i tried this:
var patron = /[a-zA-Z\D]/;
var regex = new RegExp(patron);
var v= "hello word 512";
if(v.match(regex))
{
//should not match but still dont work
}else
{
objInput.style.color = "red";
}
And also, parentheses are not being match
var patron = /[a-zA-Z\"\']/;
var regex = new RegExp(patron);
var v= "hello word 512";
if(v.match(regex))
{
//it match whenever the double quoute it followed by the single quoute'
}else
{
objInput.style.color = "red";
}
About the first example you provided, your regex /[a-zA-Z]/ checks for any character in the input string. Since it finds h in your input string, it returns true.
What you need to do is place start and end anchors, ^ and $ in your regex. New regex would look like this:
/^[a-zA-Z]+$/
You can make changes to all you regex accordingly.
To match parentheses, you need to escape them with a backslash. \( would match (, and \) would match ).
You should match the whole string, using the ^ (matches the beginning of the string) and $ (matches the end of the string) operators, for example:
/^[a-zA-Z\s]+$/.test("any string followed by numbers! 555") // will return false
This will not allow anything else than a-z chars and spaces in your string.
the match function seeks for at least ONE match in your case this is 1st symbol which is a char.
if you want ONLY chars then use /[a-zA-Z]/.test("your string")

How to check for uppercase alphabets in an input string, using jQuery

I am using following code snippet, but its not working :-(
//First four characters of input Text should be ALPHABATES (Letters)
if (($("#txtId").val()).length >= 4) {
var firstFourChars = $("#txtId").val().substring(0, 4);
var pattern = new RegExp('[^A-Z]');
if (firstFourChars.match(pattern))
isValid = true;
else
isValid = false;
}
change /[^A-Z]/ to /^[A-Z]/
example :
var a = "ABCJabcd";
console.log(a.match(/^[A-Z]{4}/));
you don't need to use substring(). Your regexp can do all the work for you. The RegExp you are using matches against characters that are NOT between A and Z. As Avinash said, ^[A-Z]{4} will match if your first 4 characters are uppercase. "^" at the beginning of your regexp tells that the following should be the beginning of the string. When placed inside square brackets, it reverts the range of characters you want to match.
The regex should be /[^A-Z]{4}/ if you want to match the 4 lowercase characters.
To detect in the middle of the big papers change /^[A-Z]/ to /[A-Z]/
Example text: " asşldla ABCJ abcd AÇALASD"
$('.Order input').change(function (){ucheck($(this).val())});
$('.Order input').keyup(function (){ucheck($(this).val())});
function ucheck(a) {
if(a.match(/[A-ZĞÜŞİÖÇ]{4}/)){
$('.Order #Error').html(' UPPERCASE');
}else{$('.Order #Error').html('Capitalize');}
}
If they need to be capital:
const startsWithCapitals = /^[A-Z]{4}/.test(string);
Or if they just need to be letters, add an i for ignore case:
const startsWithLetters = /^[a-z]{4}/i.test(string);
^ means start of the string and {number} means x copies

Categories

Resources