i want to validate numeric and allows the + (plus sign), but its not working
what i want
+63443 -> OK
8452 -> OK
s55sd -> Not OK
here's my code
var Nom = $("#addKonId1").val().split(" ").join("").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var intRegex = /^\d+$/;
if (!intRegex.test(Nom)) {
alert("wrong Number");
} else {
alert(Nom);
}
Try This
var Nom = $("#addKonId1").val().trim(" ");
var intRegex = /^\+?\d+$/;
if(!intRegex.test(Nom)) {
alert("wrong Number");
}
else{
alert(Nom);
}
DEMO HERE
The regular expression for what you're looking for is:
^\+?\d+$
Which means "a string beginning with optionally one plus sign followed by one or more digits".
Your regex right now only tests for a string beginning with one or more digit characters. Alter intRegex like so:
var intRegex = /^\+?\d+$/;
On a side note, what you're doing in your first line with the replacing can simply be done with trim():
var Nom = $("#addKonId1").val().split(" ").join("").trim();
Related
I've a variable named var text = 'Saif'
So how can I check the first character of this value (S) is a letter, number or special character??
I've already tried with the code bellow -
var text = 'Saif'
var char = /[A-Z]/g
var num = /[0-9]/g
if (text.match(char)) {
console.log("The string starts with Letter")
} else if (text.match(num)){
console.log("The string starts with Number")
} else {
console.log("The string starts with Special character")
}
It's working fine with the condition of letter and number. But I can't being able to find the special character instead of letter or number.
How can I do that?
First of all, char is a reserved word in JavaScript - best not to use it in your variable names.
Secondly, if you want to test a pattern but not actually retrieve the match, use test() rather than match().
Thirdly, your current patterns don't enforce only the first character of the string; they allow any character within it.
if (/^[a-z]/ig.test(text))
console.log("The string starts with Letter")
else if (/^\d/.test(text))
console.log("The string starts with Number")
else
console.log("The string starts with Special character")
Try this:
var text = 's2Saif'
var char = /^[A-Z]/g
var num = /^\d/g
if (text.match(char)) {
console.log("The string starts with Letter")
} else if (text.match(num)){
console.log("The string starts with Number")
} else {
console.log("The string starts with Special character")
}
Does Letter contain lowercase character? If so, let var char = /^\w/g;
Give this a try:
var format = /[ `!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
// This ↓ method will return true or false value.
if (format.test(text)) {
console.log("The string starts with Special character");
}
Trying to validate if the user has entered a name starting with a letter, is at least 8 characters long, and has at least one number in it. See the code below:-
The first two conditions I have been able to make work, its validating whether or not there's a number within. I have tried to run a function all by itself with just the number validation in it but I cant seem to get it to work. this is my latest attempt to make it work, any help would be greatly appreciated, keep in mind I am a first year student :)
function nameVerify() {
var char1;
var char2;
var index;
var NL = "\n";
var valid = false;
char1 = useNam.substr(0, 1);
char1 = char1.toUpperCase();
char2 = useNam.substr(1);
for (index = 1; index <=useNam.length; index++){
while (!valid) {
if ((char1 <"A" || char1 >"Z") || (useNam.length <8) && (char2 >=0 || char2 <=9)){
alert("alert 1");
useNam = prompt("prompt 2");
char1 = useNam.substr(0, 1);
char1 = char1.toUpperCase();
char2 = useNam.substr(1);
}
else {
valid = true;
alert("Congragulations, you entered it correctly");
}
}
}}
var useNam;
useNam = prompt("prompt 1");
result = nameVerify(useNam);
/**
* #param {string} str name to test
* #return {boolean} true if str is valid
*/
function isValidName(str) {
return /^[a-zA-Z][a-zA-Z0-9]{7,}$/.test(str) && /\d/.test(str)
}
/^[a-zA-Z][a-zA-Z0-9]{7,}$/ tests that it starts with a letter, is at least 8 characters long, and all characters are letters or numbers. /\d/ tests that it contains at least 1 number. See MDN's RegExp documentation for reference in particular about the special characters and the x{n,} syntax described there. If you allow underscores too then you could use /^[a-zA-Z]\w{7,}$/ for the first test.
Try this
valid = myString.match(/\d/).length > 0
This is a regex and will return the first number it matches or an empty array otherwise
I've got an issue with the password validation.
That's my code:
function validatePassword(){
var password = document.getElementById("password").value;
var re = /^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,16}$/;
if(!password.match(re)){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");
return true;
}
It says that its only invalid, So I thought that its because of the regex.
I asked you if you can help with everything here.
Thanks a lot for helpers!
Try this
// At least eight numbers or/and letters of English or Hebrew language
^[a-zA-Z0-9\u0590-\u05FF]{8,}$
Or
// At least eight characters: one number, one uppercase, one lowercase English letter and one Hebrew letter
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\u0590-\u05FF]).{8,}$
Or
// At least eight characters: one number and one uppercase of lowercase English or Hebrew letter
^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$
Usage:
var p = /^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$/g;
var s = "שלוםWorld2";
if(!p.test(s)){
console.log("Invalid password!");
}
Regex Demo | jsBIn Demo
Maybe you could check it twice to make it easy.
For example:
var password = document.getElementById("password").value;
var re1 = /^a-zA-Z0-9!##$%^&*]{6,16}$/;
var re2 = /[0-9]/
var re3 = /[!##$%^&*]/
if(password.match(re1) && password.search(re2) >=0 && password.search(re3) >=0){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");
I have a string which is of format 245545g65.
var value = "245545g65"
var last3Letters = value.substring(7,9); // abc
Now I want to validate whether the last three letters contains only alphabets, if it is alphabet , i want to alert it.how to alert g?
how do i do this?
assuming that "contains only alphabets" means the last three characters are a combination of the letters a-z:
var str = '245545g65';
if (/[a-z]{3}$/.test(str)){
// last three characters are any combinations of the letters a-z
alert('Only letters at the end!');
}
you can use RegEx and compare length
var re = new RegExp("[^0-9]*", "g");
var newlast3Letters =last3Letters.replace(re,"");
if(newlast3Letters.length!=last3Letters.length)
{
alert("not all alphabets");
}
else
{
alert("all alphabets");
}
you can use isNaN to check weather s string is number
if (!isNan(last3Letters))
alert(last3Letters + ' is number.')
else
alert(last3Letters + ' is not number.')
You can also do this:
var value = "245545g65"
if(value.slice(value.length-3).search(/[^a-z]/) < 0) {
alert("Just alphabets");
} else {
alert("Not just alphabets");
}
Easy:
var alpha = /^[A-z]+$/;
alpha.test(last3Letters);
This will return a boolean (true/false). Stolen from here.
I want to remove special characters from the starting of the string only.
i.e, if my string is like {abc#xyz.com then I want to remove the { from the starting. The string shoould look like abc#xyz.com
But if my string is like abc{#xyz.com then I want to retain the same string as it is ie., abc{#xyz.com.
Also I want to check that if my string has # symbol present or not. If it is present then OK else show a message.
The following demonstrates what you specified (or it's close):
var pat = /^[^a-z0-9]*([a-z0-9].*?#.*?$)/i; //pattern for optional non-alphabetic start followed by alphabetic, followed by '#' somewhere
var testString = "{abc#xyz.com"; //Try with {abcxyz.com for alert
arr = pat.exec(testString);
var adjustedString;
if (arr != null) { adjustedString = arr[1]; } //The potentially adjustedString (chopped off non-alphabetic start) will be in capture group 1
else { adjustedString = ""; alert(testString + " does not conform to pattern"); }
adjustedString;
I have used two separate regex objects to achieve what you require .It checks for both the conditions in the string.I know its not very efficient but it will serve your purpose.
var regex = new RegExp(/(^{)/);
var regex1 = new RegExp(/(^[^#]*$)/);
var str = "abc#gmail.com";
if(!regex1.test(str)){
if(regex.test(str))
alert("Bracket found at the beginning")
else
alert("Bracket not found at the beginning")
}
else{
alert("doesnt contain #");
}
Hope this helps