form code:
<form class="form" name ="custRegistration" id="custRegistration" onsubmit="return submitAlbum(this)" action="download.jsp" method="post" >
<p class="email">
<label for="budget">Expected Budget :</label>
<input type="text" name="budget" id="budget"/>
</p>
<p class="submit">
<label for="download" id="freetrail">Download 30 day free trial</label>
<input type="submit" value="Submit" />
</p>
</form>
i want to validate email-ids with the extension which are checked in the above image and block rest of the email-id extensions using javascript..any help would be appreciated??
(\w+\.)*\w+#(\w+\.)+[A-Za-z]+
this regex is email check basic.
you may use regex for this case, follow regex:
((\w+\.)*\w+)#(\w+\.)+(com|kr|net|us|info|biz)
okay , get all the values of checked items in an array (at least this much you should have been able to do by now)
now let the array be ["com","net"]
var arr = ["com","net"];
var str = arr.join("|")
var re = new RegExp("^\w+#\w+\.("+str+")$");
console.log(re);
the regex I have used is the most basic of all, you can change it according to your needs. Another answer on SO suggests "^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$" to be a more complete email validator. So you can change your second last line to :
var re = new RegExp("^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.("+str+")$");
this code will give you the regex you need to validate your email.
Now you can simply do regex test to see which emails pass your validation.
happy coding!
You can also use above regex ( aelor's )as
[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.(COM|ORG|BIZ|CO)
include your all extentions using pipe separator .
Related
I have two inputs for registration part of my project:
password <input type="password" ng-model="userData.password">
confirm password <input type="password" ng-model="userData.confirmPassword">
I am using ng-pattern to check if userData.confirmPassword is exactly same as userData.password using this:
<input ng-pattern="^userData.password$" ng-model="userData.confirmPassword">
The problem with the current code is that:
If password is stackoverflow and confirm passwors is 123stackoverflow123, the regex will still match and will return true for my form.
What regex can I place to match only if the whole text is matching?
The following change is recommended as per the documentaion:
<input ng-pattern="{{userData.password}}" ng-model="userData.confirmPassword">
I am trying to use a regular expression to validate an email address. It does not seem to be doing any validation at all. When I load the page the Submit button is disabled because of the $pristine but as soon as I type a letter the button becomes enabled. Also I am aware that the regex is only accepting upper-case at the moment. The following code is my form:
<form name="myForm" ng-hide="email" >
Insert Email : <br/>
<input type="text" name="email" ng-pattern="/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]
{2,4}$/" ng-model="insert_email" required>
<br/>
<button ng-hide="email"
type="submit"
ng-disabled="myForm.email.$pristine || myForm.email.$invalid">Submit</button>
</form>
I am not sure but I think the problem may lie with the regex itself.
take out the email in myForm.email.$pristine and in myForm.email.$invalid
to look like:
myForm.$pristine
and
myForm.$invalid
also try with ng-required instead of required
It seems to be two things. It looks like the ng-pattern expects an expression instead of a string attribute.
So you need to wrap it in a string if you want to use an inline expression.
Like so:
ng-pattern="'^[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}$'"
Also, there seems to be some issues with your regex. I changed it to this:
ng-pattern="'^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$'"
It seems to work.
Plunker Demo
In my form I have a text field in which user type date. Good habit tells me to not let user to put anything other then dgit and '-' symbol in the field. However i have a bit problem with implementing such feature. So far I Have a field which accept only digits. If user try to put in field letter, this letter is being removed. But the point is to create (DD-MM-YYYY)format so field have to accept '-' symbol. Here is my code:
<input type="text" name="test3" placeholder='DD-MM-YYYY' onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')"/>
i tried put |\- into regex but with no success. Can anyone point me where I am doing mistake?
use thie regex
/^\d{4}-\d{2}-\d{2}$|[^\d-]|-\d{2}-\d*-/
you can also
**/^\d{4}-\d{2}-\d{2}$|[^\d-]|-\d{2}-\d*-/.test(input.value)**
HTML5 has another approach for you:
<input type="date" name="test3">
The browser is responsible for the formatting of the date presentation, though.
You can try something like
<input type="text" name="test3" placeholder='DD-MM-YYYY' onkeyup="if (/[^\d-]/g.test(this.value)) this.value = this.value.replace(/[^\d-]/g,'')" onchange="validate(this)"/>
function validate(el){
var regex = /^(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-\d{4}$/;
if(!regex.test(el.value)){
alert('invalid date');
el.value = '';
}
}
Demo: Fiddle
You can do this with HTML5
see my jsfidle: http://jsfiddle.net/H7tMZ/2/
<form>
<input type="text" name="date" pattern="\d{1,2}-\d{1,2}-\d{4}" placeholder="dd-mm-jjjj"/>
<button type="submit"/>
</form>
I am using regular expression first time, and I really amazed! well, new discovery always amazement :).
I am using in JavaScript. I am using in following manner;(There are many fields and all are working perfectly fine except this phone formatting)
function validate(form) {
var phone = form.phone.value;
var phoneRegex = /^(\+|00)\d{2,3}-\d{1,2}-\d{3}-\d{4}$/g;
//Checking 'phone' and its regular expressions
if(phone == "") {
inlineMsg('phone','<strong>Error</strong><br />You must enter phone number.',2);
return false;
}
if(!phone.match(deptRegex)) {
inlineMsg('phone','<strong>Error</strong><br />Enter valid phone <br />+xxx-x-xxx-xxxx (or) <br />00xxx-x-xxx-xxxx.',2);
return false;
}
return true;
}
HTML
<div id="wrapper">
<form name="form" id="form" class="form" onsubmit="validate(this);return false">
<label for="phone">Phone:</label><input type="text" name="phone" id="phone" />
<input type="submit" value="Submit" class="submit" />
</div>
Now I am confuse that I might wrote the wrong expression but I tested it as well. I think I am mistaken to write the expression in JavaScript. Can someone help?
P.SThe following is the image from a regular expression online tester where I tested the expression.
I can see two problems with your code:
You don't have a closing </form> tag before the last </div>
You're using two different variable names for your regex: phoneRegex and deptRegex.
Once you correct those problems, the code runs fine. Have a look at it working on jsFiddle: http://jsfiddle.net/XFWGk/
If that doesn't work, the problem is probably your inlineMsg function. I'm not familiar with that one, so make sure you're using it correctly.
^(\+|00)\d{2,3}-\d{1,2}-\d{3}-\d{4}$
This matches +nn[n]-n[n]-nnn-nnnn or 0nn[n]-n[n]-nnn-nnnn
What country is that for?
As well as the 00 dial prefix, you might want to also include 011 dial prefix as used from US/Canada.
Example using Craigslist:
<form action="/search/sss" id="searchform" method="get">
<input id="query" name="query" value="car">
<select id="catAbb" name="catAbb">
<option value="sss">all for sale / wanted
</select>
<input type="radio" name="srchType" value="T">
<input type="radio" name="srchType" value="A" checked="checked">
<input name="minAsk" class="min" value="$1000">
<input name="maxAsk" class="max" value="$5000"> </td>
<input type="checkbox" name="hasPic" value="1">
<input type="submit" value="Search">
</form>
Results in this search string:
newyork.craigslist.org/search/sss?query=car&srchType=A&minAsk=$1000&maxAsk=$5000
Any way to make the ampersands '&' switch to a '+' so the url will look like this instead:
newyork.craigslist.org/search/sss?query=car+srchType=A+minAsk=$1000+maxAsk=$5000
The best example is Google's Advanced Search:
google.com/search?q=car+"new+car"+honda+OR+toyota+OR+vw+-old+site:autotrader.com+$1000..$5000
In other words, any way to manipulate a search form like Google does, to extend the query using multiple input boxes without an ampersand separating each input?
Hope i'm making sense
in javascript:
var string = "newyork.craigslist.org/search/sss?query=car&srchType=A&minAsk=$1000&maxAsk=$5000"
var newstring = string.replace('&', '+');
//newstring = "newyork.craigslist.org/search/sss?query=car+srchType=A+minAsk=$1000+maxAsk=+5000"
I'm not sure what you're getting at, but sure - regular expressions can be used to swap out characters. In javascript it would be something like this:
myUrl.replace(/\&/g,'+')
When you have
someurl?foo=1&bar=2
in your server environment you get a array with
foo => 1
bar => 2
What google does with
googleurl?q=1+2+3
if getting one var named q
q => 1+2+3
And they parse it themselves. There are several other options like mod rewrite, that in my opinion give you much cleaner urls.
But anyway this has to be generated per your javascript, since the browser form default behavior will not work that way.
As a response to your late comment, in jQuery this would look something like
$('form').submit(function(event) {
event.preventDefault();
var query = '';
$(this).find('inpup, textarea, select').each(function() {
query += '+'+$(this).val();
});
query = escape(query.substr(1));
//debug
alert(query);
});