Regex Expression validation in javascript - javascript

Regex to check the first character is in uppercase and allow only alphanumeric,not allow the special charcter.
Thank you Advance
function checkName(val) {
var alpha = document.getElementById(val).value;
var filter = /^[a-zA-Z0-9 ]*$/;
if (!filter.test(alpha)) {
alert("Please Enter Alphanumeric Only");
return false;
}
return true;
}
i Used This its working properly for checking alphanumeric but for
first character uppercase its not working
where can i modify my regex expression or any solution.

try
/^[A-Z][a-zA-Z0-9]+/
For example
/^[A-Z][a-zA-Z0-9]+/.test("Asd");

Related

javascript .match function

I have the following javascript .match script to allow telephone numbers in form submit
var number = jQuery('#phone-number').val();
if ((number.match(/(\d)\1\1\1\1\1/))
|| (number.match(/(\d)(\d)\1\2\1\2\1\2/))
|| (number.match(/123456|234567|345678|456789|567890|987654|876543|765432|654321|543210/))
|| (!number.match(/^(0\d{8,10})?$/))) {
alert("Please supply a valid phone number");
return false;
}
Currently, it doesnt allow a SPACE between numbers.. I'm no good at regex and was wondering if someone could tell me how I allow a SPACE between any number using the script above?
thanks
Craig.
If you want to specify any number of spaces between each character, you can use \s*.
\s stands for whitespace character and * for any number of those
E.g.
\s*(\d)\s*\1\s*\1\s*\1\s*\1\s*\1\s*
const regex = /\s*(\d)\s*\1\s*\1\s*\1\s*\1\s*\1\s*/;
const tel1 = '111111';
const tel2 = ' 1 1 1 1 1 1';
console.log(regex.test(tel1));
console.log(regex.test(tel2));
Ugly, but:
if ((number.match(/(\d)\s*\1\s*\1\s*\1\s*\1\s*\1\s*/))
|| (number.match(/(\d)\s*(\d)\s*\1\s*\2\s*\1\s*\2\s*\1\s*\2\s*/))
|| (number.match(/123456|234567|345678|456789|567890|987654|876543|765432|654321|543210/))
|| (!number.match(/^(0(?:\s*\d\s*){8,10})?$/))) {
alert("Please supply a valid phone number");
return false;
}
For 1 space only replace \s* with \s?
You can remove all spaces from a string with
str = str.replace(/\s/g, '');
Then you can use your existing code.

validation function doesn't work

I have created a java script function, it should validate the input character that should contain 10 characters and can contain alphanumeric characters, but this function does not work, please help me
function ValidateNIC(id)
{
var letters = /^[0-9a-zA-Z ]+$/;
while(id.value.length==10)
if(id.value.match(letters))
{
return true;
}
else
{
alert('NIC must have alphanumeric characters only or should contain 10 charaters');
id.focus();
return false;
}
}
With your code as it stands, if the length is not 10, then nothing else happens. A better approach might be:
if ((id.value.length == 10) && id.value.match(letters)) {
return true;
}
alert("NIC must ...");
id.focus();
return false;
You can put all the conditions for validation in Regex like ^[a-zA-Z0-9]{10}$. Note that additional {10} in the regex pattern string for creating a match only when the length is 10 exactly.
Then you can make use of the Regex Object test method, which test the regex pattern against a string and returns true if the match is successful and false otherwise.
Complete modified snippet below with positive and negative test cases.
function ValidateNIC(id){
var aphaPattern10 = /^[a-zA-Z0-9]{10}$/g;
var result = aphaPattern10.test(id.value);
if(!result){
alert('NIC must have alphanumeric characters only or should contain 10 charaters');
//id.focus();
}
return result;
}
var testObjPass = { value : "012345678a"}
console.log(ValidateNIC(testObjPass));
var testObjFail = { value : "012345678a21312"}
console.log(ValidateNIC(testObjFail));
The following code checks the following
NIC must have alphanumeric characters only or should contain 10 charaters.
So if it is only 10 characters then it will not alert else, it will test the regex. Considering id is an object with key value
function ValidateNIC(id)
{
var letters = /^[0-9a-zA-Z ]+$/;
if(id.value.length!==10){
if(id.value.match(letters))
{
return true;
}
else
{
alert('NIC must have alphanumeric characters only or should contain 10 charaters');
id.focus();
return false;
}
}
}

Javascript check for 1 special character and 2 digits

How to create a javascript validation for a password field which must contain at least one special character and at least two digits ?
Exact regular expression that perfect match to your query is below, it is tested ...
^(?=.*?[0-9].*?[0-9])(?=.*[!##$%])[0-9a-zA-Z!##$%]{8,}$
function check(str){
var temp = str;
if(/^[a-zA-Z0-9- ]*$/.test(str) == false && temp.replace(/[^0-9]/g,"").length>1) return true;
return false;
}

Validation to allow space character only when followed by an alphabet

I am trying to validate the textbox which has to allow space character only when followed by any alphabetic character. My code fails when only a space character is inserted. What is the mistake in my code. Suggestions pls..
javascript :
function validate() {
var firstname = document.getElementById("FirstName");
var alpha = /^[a-zA-Z\s-, ]+$/;
if (firstname.value == "") {
alert('Please enter Name');
return false;
}
else if (!firstname.value.match(alpha)) {
alert('Invalid ');
return false;
}
else
{
return true;
}
}
view:
#Html.TextBoxFor(m => m.FirstName, new { #class = "searchbox" })
<button type="submit" onclick="return validate();">Submit</button>
Conditions I applied :
Eg: Arun Chawla - condition success
Eg: _ - condition fails (should not allow space character alone)
try following regex
var alpha = /^[a-zA-Z-,]+(\s{0,1}[a-zA-Z-, ])*$/
First part forces an alphabetic char, and then allows space.
May be using the "test" method like this:
/^[a-zA-Z-,](\s{0,1}[a-zA-Z-, ])*[^\s]$/.test(firstname)
Only returns true when it's valid
Here's a link
I found Akiross's answer to be the best fit for me.
([a-z] ?)+[a-z]
This will allow the space between every character,number and special character(#).
pattern to be followed:
['', [Validators.email,
Validators.pattern('^[[\\\sa-z 0-9._%+-]+#[\\\sa-z0-9.-]+\\.[\\\sa-z]{2,4}]*$')]],
output:
e 2 # 2 g . c o
if(!/^[A-Za-z\s]+$/.test(name)) {
errors['name'] = "Enter a valid name"
}

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+/

Categories

Resources