?
Solved

Check for "@" symbol as part of a alert in a domain prefix for web form.

Posted on 2009-04-13
6
Medium Priority
?
186 Views
Last Modified: 2012-05-06
Greetings Experts;

To resolve a problem I had with a form, I am using the example code from a previous post how to strip urls from form field before or on submit
I have modified the code just to check the prefix and not the suffix. I would also like to alert on the @ but have been baffled on how to add this.  Any suggestions? I have listed the modifed code.

Thanks

Dave

<script type="text/javascript">
function checkIt(o){
	if( /(<a +href|http:|www|@\.)/i.test(o.value)){
		alert('Please do not include the prefix "www, http, etc.. ');
		o.focus();
		return false;
	}
return true;
}
</script>

Open in new window

0
Comment
Question by:Davidjevans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24135927
You can do one of two things either do the test twice, once for the prefix and then again for the @. Or you could use the exec method of the regexp object. This method can check for multiple matchs as show in this example from the manual.
function matchDemo()
{
  var s;
  var re = new RegExp("d(b+)(d)","ig");
  var str = "cdbBdbsbdbdz";
  var arr = re.exec(str);
  s = "$1 contains: " + RegExp.$1 + "<BR>";
  s += "$2 contains: " + RegExp.$2 + "<BR>";
  s += "$3 contains: " + RegExp.$3;
  return(s);
}

Open in new window

0
 

Author Comment

by:Davidjevans
ID: 24139053
Thanks Graceful;
 
     I think my main issue is the fact that the "@" is not recongnized in the string. Also, to which manual are you referring to? I am a project manager turned programmer on this project so my only formal training goes back 25 years to assembler level language.

Regards

Dave
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24143160
I ran your code as below and tested with <a @. it ran as you wanted. ie I see no problem

<script type="text/javascript">

function checkIt(o){
        if( /(<a +href|http:|www|@\.)/i.test(o.value)){
                alert('Please do not include the prefix "www, http, etc.. ');
                o.focus();
                return false;
        }
return true;
}

</script>

<input type=text  onblur="alert(checkIt(this))">
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24145320
I modified the prev function to test for your specifications and no there is no problem with the @. Try each of your types of strings in this function and see if it will work for you.
function matchDemo()
{
  var s;
  var re = new RegExp("(^.*@.*$)|(^http:.*$)|^(www)");
  var str = "www.alfega.com";
  var arr = re.exec(str);
  s = "$1 contains: " + RegExp.$1 + "<BR>";
  s += "$2 contains: " + RegExp.$2 + "<BR>";
  s += "$3 contains: " + RegExp.$3;
  alert(s);
}

Open in new window

0
 

Author Comment

by:Davidjevans
ID: 24252834
Sorry for the delay in getting back to this. Priority projects jumped in.  

Gwynforweb. I think if you tried it without a period in front of the @ (I am guessing this is why you did not see the issue) you will find this does fail. For whatever reason, the group I am coding this for say's the get periodic "@" signs in the responses. As if they are entering an e-mail address.  

Graceful Penguin, thank you for the suggestion, however I am afraid it is a bit beyond me. I am a former training manager, turned project manager, turned programmer. (We do what we can to stay employed these days). Can you be a bit more descriptive for me?

Thanks

Dave
0
 
LVL 9

Accepted Solution

by:
Graceful_Penguin earned 600 total points
ID: 24257997
OK I modified the code to do exactly what you wanted. But let me explain what it does and why.
The attached code first compiles the regular expersion :
var re = new RegExp("(^.*@.*$)|(^http:.*$)|^(www)");
then it runs the regexp on o.value :
re.exec(o.value);
Then it tests each of the result strings to see if a match was found :
if ((RegExp.$1 != '') || (RegExp.$2 != '') || (RegExp.$3 != ''))

Let me explain what happens here, as you can see the regexp string "(^.*@.*$)|(^http:.*$)|^(www)" is divided into three separate tests for each of the conditions. The result of each of these test are stored in an array : RegExp.$ also each element can be access by RegExp.$N were N is the number of the test . (like I did in the code)

The reason I test each subset is to show you how you can supply more information to the user because you know exactly what part of the test failed. (I did not code that part of the function you can add that if you want to when you want to)

I hope you understand what I tried to explain. Feel free to ask if there is something you don't understand.
function matchDemo(o)
{
  var re = new RegExp("(^.*@.*$)|(^http:.*$)|^(www)");
  re.exec(o.value);
  if ((RegExp.$1 != '') || (RegExp.$2 != '') || (RegExp.$3 != ''))
  {
    alert('Please do not include the prefix "www, http, etc.. or an @');
    o.focus();
    return false;
  }
  else
    return true;
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question