Validation to allow space character only when followed by an alphabet - javascript

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"
}

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.

How can I match everything not in a character set with regex and jQuery?

I'm trying to exclude everything except [a-z] [A-Z] [0-9] and [~!#$].
if( $("#name").val().test(/[^a-zA-Z0-9~!#$]/)) {
alert("Disallowed character used.");
return false;
}
I thought test would return true if it finds a match and that because I included the caret symbol it should match everything that is not in my expression.
However input such as this works: jAcK%%$21#x
When it shouldn't work because it has symbols I'm trying to disallow.
Where have I gone wrong?
Thanks
Use match instead of test, since test is function of RegExp instead of String
var hasBadInput = !!"jAcK%%$21#x".match(/[^a-zA-Z0-9~!#$]/) //true
Or reverse the position
(/[^a-zA-Z0-9~!#$]/).test("jAcK%%$21#x") //returns true
Regexp.prototype.test() should be called on the regex.
var $name = $("#name");
$name.on("keyup", function(e) {
if(/[^a-zA-Z0-9~!#$]/.test($name.val())) {
console.log("Nope");
return false;
} else {
console.log("Good");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="name" >

Regex requirement to have # symbol only once in a string

I am new to javaScript and the problem I am facing is that I have to match the # symbol and make sure it is allowed only once in a string. To do so I have written the following regex.
var regexpat=/[#]{1}/;
if(regexpat.test(valu))
testresults = true;
else
{
alert("Please input a valid email address!");
testresults = false;
}
My regex is working for the following input value: abcd#abc#abc.com. However, if I provide the input value as "abcd#"#abc.com it is not throwing the alert error message.
How can I change my regex so that it will work for "abcd#"#abc.com?
Your regexp just tests whether there's a single # in the string, it doesn't reject more than one. Use:
var regexppat = /^[^#]+#[^#]+$/;
This matches an # that's surrounded by characters that aren't #.
var valu;
var regexpat = /^[^#]+#[^#]+$/;
while (valu = prompt("Enter email")) {
if (regexpat.test(valu))
console.log(valu + " is valid");
else {
console.log(valu + " is invalid");
}
}
The easy way could also be to use the split("#") for this:
var value = 'abcd##';
if(value.split("#").length === 2){
testresults = true;
}else{
alert("Please input a valid email address!");
testresults = false;
}
Just split your string with # and since you require only one occurrence of # there must be an array of length 2 so you can compare the array with length 2. If the array length is greater than 2 then there are more than one occurrence of #
E-Mail regex is much more, than just the occurence of just one # character. This is the email-regex specified in the W3C Spec (e.g. used for <input type="email">):
/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

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;
}
}
}

Regex Expression validation in 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");

Categories

Resources