Does Len function only evaluate numerical results? - forms

Why does the following code not output "Error" if the form is submitted with a blank field? Does Len only evaluate numerical values?
<cfif NOT Len(Trim("Form.myField"))>
<cfoutput>Error</cfoutput>
</cfif>
The following also does not evaluate as expected:
<cfif Len(Trim("Form.myField")) IS 0>
<cfoutput>Error</cfoutput>
</cfif>
HTML:
<input type="text" name="myField" value="">

Because it's evaluating the literal string "Form.myField", which is not length 0.
Try: <cfif len(trim(form.myField)) EQ 0>

are you sure you're supposed to pass in the parameter in quotes within the trim function? it may be literally trimming the string "Form.myField"

Related

How to append a character while using the conditional statement in sightly?

I need to set the href value depending on the nullity of sling:vanityPath. The hit.properties.sling:vanityPath returns the string value for sling:vanityPath property. I need to append a / before the hit.properties.sling:vanityPath value. Is it possible to do that in the following code or do I have to test it twice, but that comes with code repetition?
<a href="${hit.properties.sling:vanityPath == null? hit.path : hit.properties.sling:vanityPath # extension='html'}"/>
Concatenation or binary operators are not supported in HTL. You can either use prependPath or double test for the slash.
I don't think you can do it in one line. But you can try defining a variable for the concatenated value instead of duplicating code for the a tag. Also, do you have to check for null specifically? can't you just reverse the conditions and check if the value exists?
<sly data-sly-test.concatenatedURL="${['/', hit.properties.sling:vanityPath] # join = ''}"/>
<a href="${hit.properties.sling:vanityPath ? concatenatedURL : hit.path # extension='html'}"/>
You can use # join to concat an array:
<a href="${hit.properties.sling:vanityPath ? ['/', hit.properties.sling:vanityPath] : hit.path # join ='', 'extension='html'}"/>

Why this line stops Sphinx search?

I use sanitizing from example: Barryhunter's
But when I use the line:
$q = preg_replace('/[^\w~\|\(\)\^\$\?"\/=-]+/',' ',trim(strtolower($q)));
then Russian search don't works! Only English.
What the reason? How I should use sanitizing?
This is my piece:
<HTML>
<BODY>
<form action="" method="get">
<input name="q" size="40" value="<?php echo #$_GET['q']; ?>" />
<input type="submit" value="Search" />
</form>
<?php
require ( 'sphinxapi.php' );
$sphinx = new SphinxClient;
$sphinx->SetServer('ununtu', 9312);
$sphinx->open();
$sphinx->SetMatchMode (SPH_MATCH_EXTENDED);
$sphinx->setFieldWeights(array(
'title' => 10,
'content' => 5
));
$sphinx->SetRankingMode(PH_RANK_WORDCOUNT);
$sphinx->SetSortMode(SPH_SORT_RELEVANCE);
$sphinx->setLimits(0, 10, 200);
$sphinx->resetFilters();
$q = isset($_GET['q'])?$_GET['q']:'';
$q = preg_replace('/ OR /',' | ',$q);
// $q = preg_replace('/[^\w~\|\(\)\^\$\?"\/=-]+/',' ',trim(strtolower($q)));
if(isset($_GET['q']) and strlen($_GET['q']) > 1)
{
$result = $sphinx->query($sphinx->escapeString($q), '*');
...
Assuming your input string is utf-encoded you use non-unicode preg_replace. Add 'u' in the end, e.g.:
$q = preg_replace('/[^\w~\|\(\)\^\$\?"\/=-]+/u',' ',trim(strtolower($q)));
Specifically that regex is stripping anything that is not a 'word' char, or a predefined list of syntax/punctuation chars.
The PREG definition of word (the \w ) is
A "word" character is any letter or digit or the underscore character,
that is, any character which can be part of a Perl "word". The
definition of letters and digits is controlled by PCRE's character
tables, and may vary if locale-specific matching is taking place. For
example, in the "fr" (French) locale, some character codes greater
than 128 are used for accented letters, and these are matched by \w.
http://php.net/manual/en/regexp.reference.escape.php
So possibly in English locale (or other western European for example), hence many Russian chars are not considered a word char, and stripped.
(if your pages are in UTF8, then may also need the /u as mentioned by other answer)

Convert string to utf-8 unicode in ColdFusion

I need to convert a string to a UTF8 encoded format and I'm not sure how to proceed.
Is there any function within ColdFusion to convert a string into UTF-8, such as on this website?
For example, typing in "stackoverflow.com/questions/ask" into the above website gives the result:
\x73\x74\x61\x63\x6B\x6F\x76\x65\x72\x66\x6C\x6F\x77\x2E\x63\x6F\x6D\x2F\x71\x75\x65\x73\x74\x69\x6F\x6E\x73\x2F\x61\x73\x6B
I am not very familiar with encoding, however my instructions were to encode a string to UTF-8. The example I was given gave an encoded result of the below for example.
/re/r/434/t//4r3/t434/4t/t3/3/4t/43tt/53/
I am not sure if this is a real representation of an encoded string or if it was just typed out to give a visual example. Is there a format that looks like that? And is it different than the format from the first example?
Thank you!
I think you can use a combination of CharsetDecode() and CharsetEncode() to accomplish this.
<cfset my_string = "test">
<cfset binary_my_string = CharsetDecode(my_string, "ASCII")>
<cfset utf8_my_string = CharsetEncode(binary_my_string, "utf-8")>
You'd just need to substitute the correct initial encoding for "ASCII" in my example.
<cfset str = "stackoverflow.com/questions/ask">
<cfset hexStr = "">
<cfloop index="i" from="0" to="#len(str)-1#">
<!--- Pick out each character in the string. Remember that charAt() starts at index 0. --->
<cfset ch = str.charAt(i)>
<!--- The decimal value of the Unicode character. ColdFusion uses the Java UCS-2 representation of Unicode characters, up to a value of 65536. --->
<cfset charDecVal = asc(ch)>
<!--- The decimal value of the character, upper-casing the letters.--->
<cfset charHexVal = uCase(formatBaseN(charDecVal,"16"))>
<!--- Append the characters together into a Hex string, using delimiter '\x' --->
<cfset hexStr = hexStr & "\x" & charHexVal>
</cfloop>
<cfscript>
writeoutput(hexStr);
</cfscript>

Google forms Regular Expressions

i'm creating a survey in google forms and cant find any regular expressions for a pin code entry.
The User is being asked a question and can enter 2 pin codes in two text fields.
I need the Regular expression that contains 4 digits with numbers from 0-9.
Example:
Textbox1: 1234
Textbox2: 4321
Any ideas?
Try \d{4}
Also set your Regular Expression to Matches
[0-9]{4}
This should be your regular expression.
function validate() {
var textField = document.getElementById("textbox1").value;
var regex = /[0-9]{4}/g;
alert("Valid input: " + regex.test(textField));
}
<input type="text" id="textbox1">
<input type="button" onclick="validate()" value="Validate">

openbd cfloop over a date

I'm trying to convert my sites from CF8 to openBD. I have a cfloop in a site that loops over a date range.
In essence, I want to insert a new record into the db for every 2 weeks (step) of a date range (from and to)
my loop looks like this...
<cfloop
from = "#form.startDate#"
to = "#form.endDate#"
index = "i"
step = "#theStep#"
>
This works perfectly in CF8, in openBD, I get this error...
Data not supported: value [11/05/09] is not a number
Any ideas of a work around?
Thx
Your problem lies in not checking for ambiguous locale dependent date strings from your FORM.
A more robust version would be this:
<cfset SetLocale("English (US)")> <!--- set expected input locale here --->
<cfif LSIsDate(form.startDate) and LSIsDate(form.endDate)>
<cfset theStep = 14>
<cfloop
from = "#LSParseDate(form.startDate)#"
to = "#LSParseDate(form.endDate)#"
index = "i"
step = "#theStep#"
>
<!--- do stuff --->
</cfloop>
<cfelse>
<!--- output some error message --->
</cfif>
It would be helpful to restrict people to entering unambiguous date formats into the FORM, like "yyyy-mm-dd".
The "value is not a number" error comes from the fact that the loop still goes over numbers, even if you feed it dates. It uses a numerical representation of these dates then, but they must be valid and intelligible for that to work.
I can't see your code, but here's my first suggestion:
<cfset current = [your begin date]>
<cfloop condition = "datecompare(enddate, current)">
[do stuff]
<cfset current = dateadd('d', 14, current)>
</cfloop>
HTH.
As Ben says, your code isn't there - you need to use the 101 010 icon to create a code block for it.
Here's another solution which should work:
<cfloop index="Current" from="#parseDateTime(StartDate)#" to="#parseDateTime(EndDate)#" step="14">
[do stuff]
</cfloop>