Jquery Validator - Conditional Validations on a single field - javascript

I have a Forgot Password form where we have a single input field in which the user can put either the email or the phone number.
Now my validation should work on based on the user content. If the user puts an email, the email validation should run and if the user puts mobile number, the mobile number validation should run.
Can someone please help in such a scenario. Tried google-ing but could not find any such scenario.

var value=document.getElementById(id).value;
var email = /^[a-zA-Z\._-]+#[a-zA-Z\.-]+\.[a-z]{2,6}$/;
var phone = /^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}98(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$/;
if(email.test(value)) {
//email
} else if (phone.test(value)) {
//phone #
} else {
//invalid
}
Try that. Since the phone # regex is kinda complicated, use http://regexlib.com/ to find a simpler one.

You could write a regex that check if the input is a phone number / e-mail. But if the user inputs something wrong, you might have that regex fail. Here's a regex for phone numbers: example of a regular expression in jquery for phone numbers, if the input matches this regex, use the phone validation. If not, use the e-mail validation.

Related

Validate email address copied from Outlook

Want to validate email fields where user can enter any kinds of emails .
My validation check using regex fails when user copy pasted email address from Outlook. Email address copied from Outlook looks different as
M, Karan <karan.m#outlook.com>
And my input looks like m.karan#yahoo.com,M, Karan <karan.m#outlook.com>. How to validate this input.
++adding code snippet
Am passing individual emails by splitting it with comma separator to below function,
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
This logic broke when emails copied from outlook available in input element.
Looking forward for help.
1. Solution
Use regular expression like this (but concrete this regex is not the best of the world :) ):
^(([a-zA-Z\-0-9,.: ])*<){0,1}(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))>{0,1}$
2. Solution
Write JavaScript function that parse email and then use your regular expression. Here is example of parse function:
function parseEmail(origin) {
return origin.substring(origin.lastIndexOf('<')+1, origin.lastIndexOf('>'));
}

How to write JavaScript form validation regex

I'm really new to HTML and JavaScript and I'm working on using regex to validate input...
I used /^[a-z]+$/i.test (firstName) to make sure the entered first name was all characters.
if( /^\d{8}$/.test(idNum)) doesn't seem to be working. I'm trying to make sure they enter an 8 digit number.
I've got to validate some other fields like Address, city, state, zip and phone number. Any resources or examples would be great!
Thanks!

Regex for preventing 'test#xx.xxx' in email fields

I have a regex to validate email fields. it accepts any string before '#' character. I want to prevent user entering junk email ids like 'test#xxx.com' or 'spam#xxx.xx.xx' etc.
I am using data abide for validations and below is the regex:
/[a-zA-Z0-9_]+(?:\.[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+)*#(?!([a-zA-Z0-9]*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*\.))(?:[A-Za-z0-9](?:[a-zA-Z0-9-]*[A-Za-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?/g
Don't bother.
The ONLY way of confirming email addresses is sending a confirmation email and wait for the answer.

Restrict input some personal information

I want to check what users type in Textarea.
Actually, how can I restrict typing phone numbers and e mail addresses in description box?
So for example:
Hi, I am selling a Bugatti Veyron
Age: 2010
Color: Black
You can contact me on 066/656-656 or 055646646
or via mail mesell#domain.com
If someone tries to enter something like this I want to automatically remove
personal contact details.
So, please help, how can I do it?
Use Regex and do something like this :
Here's an example (jsFiddle)
HTML
<textarea></textarea><br>
<button>Test</button><br>
<span class="result"></span>
Javascript:
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
$('.result').html(re.test(email));
if(re.test(email)){
$('.result').html("Contain email");
} else {
$('.result').html("Do not contain email");
}
}
$('button').click(function(){
validateEmail($('textarea').val());
});
Note that I only look for email. But you can use other Regex to look for phone, you just have to search for something like "javascript regex phone" on Google.
you can try Regex as suggested, but take into consideration it's very hard to stop a phone number from being entered(unless you stop ALL numbers or know the exact form of the number taking place).
For example, stopping a xxx-xxxxxxx number is easy, but the user can type each digit with a space after it, which makes it much harder to stop(unless you again remove the option to type numbers.
As for emails, a simple regex to find a # followed by some text, a dot and 2 or 3 characters normally finds emails pretty easily. be advised people can still be creative in the way they put their emails(AT instead of # for example).
this should all be done server side, you can use javascript on the clientside to make it UI friendly.
For email validation:
<input type="email" name="email">
Here is an example: http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_input_type_email
For Phone number validation:
use regex, it should be straight forward though.
Or just try isNAN(value) where value is the phone number enterd, will tell you if this is a number of not and then you can also put a check on number of digits.
Hope this helps!

regular expression with if statements

I have this as my regular expression:
var email = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
And this is my if statement:
if($('#email').val() ==""){
$('#emailErrorMsg').text("Please enter a valid email address.");
}
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
else($('#emailErrorMsg').text("Please enter a valid email address."));
});
When I type in a valid email address it says "OK". However, if I enter just some text for example it still says "OK" when I want it to say "Please enter a valid email address". Anyone any idea. By the way, I'm still an amatuer at this stuff!
The main problem is that you have a ? at the end of the regex, following parentheses that enclose the entire pattern. This effectively makes the entire match optional, so the regex will literally match anything.
Note also that you are testing the literal string #email, not the value of the #email element. Make sure you pass the appropriate string to test().
I see that you have jquery tag, so take a look to JQuery validate plugin, it will be better than a simple regex.
But if you still want regex, see Validate email address in JavaScript?
Validating emails is hard. The fully correct regex is a true monstrosity that you can see (if you dare) at http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html which probably isn't what you want.
Instead, you have a few options. Use a regex that matches 99% of emails, do it server side with an email validation library, or implement a finite state machine to parse it correctly. The state machine is probably too bulky (although allows neat stuff like suggestions for possible typos) and doing it all server side -- which you better be doing anyway (what if someone has JavaScript disabled?) -- loses the benefits of as-you-type checking.
That leaves a simpler regex that doesn't match all legal emails, but matches enough that the chances of someone registering with one that it doesn't are really slim.
The regex from Validate email address in JavaScript? should do the trick pretty well:
/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\
".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Also, you made a small typo:
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
is testing against the string '#email' -- not the element with the ID 'email'. Change that to:
else if(!email.test($('#email').val())) {
$('#emailErrorMsg').text("OK");
}
There's a little typo in your regex. Try this:
var email = /^([\w-\.]+)#([\w-]+\.)+[\w-]{2,6}?$/;
That should also handle the .museum case

Categories