URL check in actionscript - actionscript-3

I am a novice in actionscript. I am trying to make an if condition for URL check. I want to call a method if a string contains a URL:
Regular expression
var pattern:RegExp = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;
Pseudo code
if(string.contains(pattern)){
//do something
}
How can make this if condition in actionscript?

You can validate a URL/(any String) in actionscript by using RegExpValidator, there are lots of way to validate data(string) but it is best way to validate all data(string). it follow all the concept of javascript's RegExp
you can learn more about RegExpValidator on http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/validators/RegExpValidator.htm, Check this url
Also its live demo of regular Expression in actionscript
http://www.regexr.com/
http://ryanswanson.com/regexp/#start
http://probertson.com/resources/projects/actionscript-regexp-test/RegExpTestHarness.html
may any of the one will help you!!!

if (string.search(pattern) != -1) {
//do something
}
If there is no match, search returns -1.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#search%28%29

Related

Display value + text using jQuery

I am a total beginner. I have a form in HTML and am trying to calculate a specific value using jQuery. I want this value to be displayed in paragraph <p id="final"></p> under the submit button, but am actually not sure, why my code isn't working.
jQuery(document).on("ready", function() {
jQuery("final").hide();
jQuery("#form").submit(function(e){
e.preventDefault();
const data = jQuery(this).serializeArray();
/*
some calculations
*/
$('#final').html($('#final').html().replace('','result + " text"'));
jQuery("#final").show();
}
}
Do you have any idea, what could I be doing wrong??
You've got a several issues here.
Firstly, don't mix jQuery and $. If you're using the former, it's normally to avoid jQuery's alias, $, from conflicting with other code that might use $.
Secondly, you don't actually do any calculation (from what I can see in your code), so I'm not sure what you're wanting to output. I'll assume you're going to fill that in later.
Thirdly, jQuery('final').hide() is missing the # denoting you're targeting by element ID.
Fourthly, the line
$('#final').html($('#final').html().replace('','result + " text"'));
...doesn't quite do what you think it does. For one thing, it makes no reference to your data variable. And running replace() on an empty string doesn't make much sense.
All in all I'm guessing you want something like (note also how I cache the #final element - that's better for perforamnce):
jQuery(function() { //<-- another way to write a document-ready handler
let el = jQuery('#final');
el.hide();
jQuery("#form").submit(function(e){
e.preventDefault();
const data = jQuery(this).serializeArray();
let calc = 5+2; //<-- do what you need to here
el.html(calc).show();
}
}
Guessing result is your variable and your above code is your current status, you should fix the html replacement to something like (depending on your acutal usecase):
$('#final').html(result + " text"));

Asp.Net Core 2.1.0-preview1-final: #Html.ActionLink() is not working for string.Format()

<div data-collapse class="left-justify" id="requirements">
#Html.Raw(string.Format(#_stringLocalizer["RegisterNoticeMessage"], #Html.ActionLink(#_stringLocalizer["RegisterLinkDisplayName"], "Register")))
</div>
In this piece of code, #Html.ActionLink() is returning Microsoft.AspNetCore.Mvc.Rendering.TagBuilder instead of returning anchor element containing URL path to the specified action.
What is the right way to use #Html.ActionLink() in string.Format(). Or, do I missing anything, here?
The helper method Html.ActionLink always returns a TagBuilder object. When you pass such an object into a string parameter, the ToString() method will be called, resulting in your observed output (the class name: "Microsoft.AspNetCore.Mvc.Rendering.TagBuilder").
It seems to me you are trying to create a hyperlink in a rather weird way. Have you tried using the Url.Action helper method? This method returns a plain old string, ready to plug into any href attribute.
E.g. this code would be equivalent of what you're trying to achieve:
#Html.Raw(
string.Format(_stringLocalizer["RegisterNoticeMessage"],
"" + _stringLocalizer["RegisterLinkDisplayName"] + "")
)
Sidenotes:
It is possible get the string value of a TagBuilder, as illustrated in this post.
No need to repeat # when you're already working in Razor/C# context.
Be extremely careful when using Html.Raw as it might result in XSS vulnerabilities.

Trim not working in Coldfusion 8.0

I am trying to trim a string returned by one of my Coldfusion component but whatever I do Coldfusion add line feed at the start of the String without any reason resulting in an error in my Javascript. Do you have any idea of what's wrong with this code?
function back(){
window.location = <cfoutput>"#Trim(Session.history.getBackUrl())#"</cfoutput>;
}
The code above produce the following peace of HTML:
function back(){
window.location = "
http://dummy_server_address/index.cfm?TargetUrl=disp_main";
}
Looking at the Coldfusion specs here is the trim definition :
A copy of the string parameter, after removing leading and trailing spaces and control characters.
So it should have done the job! I am therefore wondering how to do that properly, I don't want to use replace or some similar function.
EDIT : very surprisingly this is working... but I don't like this solution, so if you have any other idea, or at least explanations about this behaviour.
<cfset backUrl = Session.history.getBackUrl()>
function back(){
window.location = <cfoutput>"#backUrl#"</cfoutput>;
}
Make sure your History component has output disabled. i.e:
<cfcomponent output=false >
Then make sure the getBackUrl function (and every other function) in the CFC also has output=false set.
Also, don't forget to use JsStringFormat on the variable, to ensure it is appropriately escaped:
<cfoutput>"#JsStringFormat( Session.history.getBackUrl() )#"</cfoutput>
Otherwise, there's a potential risk for JavaScript injection, or just JS errors, if the URL happens to contain ".
I've tested your current code and it works fine for me, I suspect that your CFC might be returning more then you think, which I obviously can't duplicate. I would personally always ensure that the component returns 'clean' results rather then removing junk characters after the fact :)
I have had similar issues in the past and it has always turned out to do with cfoutput, never got to the bottom of it. As a starting point I would rewrite this way and see if it makes a difference...
<cfset variables.stWindowLocation = '"' & Trim(Session.history.getBackUrl()) & '"'>
<cfoutput>
function back() {
window.location = #variables.stWindowLocation#;}
</cfoutput>

how to skip undefined properties in JSON?

When I parse JSON fields coming from google maps, etc., it is a mess. Because they are not made specifically for my script I have to verify many details, epecially because the addresses are different in every country.
Short question: when the script finds a undefined property the script breaks...error..
How can I verify the property is defined?
if(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality != null) {
/***do something***/
}
Something like that doesn't seem to solve the problem. Why?
In JavaScript, accessing a property of an object that does not exist returns undefined, not null - heck, you said it in the title.
So, assuming that all the previous properties do actually exist, you can check that the Locality property exists using typeof, like this:
if(typeof (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.
Locality) !== 'undefined') {
/***do something***/
}
Or, (I think) you can use hasOwnProperty():
if (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.hasOwnProperty('Locality'))
{
/*** do something ***/
}
First, In javascript you can use "try / catch" like java or other programming language, this can let your code continue running if something goes wrong...
for your issue, you can test that :
if (typeof(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality)
&&
data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality.length>0) {
}

How can I include special characters in query strings?

URL http://localhost/mysite/mypage?param=123 works fine. However, if I want to put some special characters in param, like ?, /, \, then the URL becomes http://localhost/mysite/mypage?param=a=?&b=/ or http://localhost/mysite/mypage?param=http://www.example.com/page2?a=\&b=... which won't work. How do I resolve this issue?
You have to encode special characters in URLs. See: http://www.w3schools.com/tags/ref_urlencode.asp
You need to encode the query parameters before combining them to form a url. The function needed here is encodeURIComponent.For example,
the url you need to create is:
http://localhost/mysite/mypage?param=a=?&b=/
Now, assuming that ? and / comes as variables, you need to encode them before putting in the url.
So lets create your url using this function(I am expecting two query parameters):
var q1 = "a=?"; //came from some input or something
var q2 = "/"; //came from somewhere else
var faultyUrl = "http://localhost/mysite/mypage?param="+ q1 +"&b=" + q2;
// "http://localhost/mysite/mypage?param=a=?&b=/"
var properUrl = "http://localhost/mysite/mypage?param="+ encodeURIComponent(q1) +"&b=" + encodeURIComponent(q2);
//"http://localhost/mysite/mypage?param=a%3D%3F&b=%2F"
This function is in basic JS and supported in all the browsers.
Easy way to pass QueryString value with special character using javascript:
var newURL=encodeURIComponent(uri);
window.location="/abc/abc?q="+newURL;
In JavaScript you can use the encodeURI() function.
ASP has the Server.URLEncode() function.
You can use HttpServerUtility.UrlEncode in .NET.
You need to use encode special characters, see this page for a reference.
If you're using PHP, there's a function to do this, called urlencode().
I did below, it works fine.
const myQueryParamValue = "You&Me";
const ajaxUrl = "www.example.com/api?searchText="+encodeURIComponent(myQueryParamValue)
You need to substitute the characters with URL entities.
Some information here.