I have the following java script function in my perl document:
function emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,4}$/;
var x = document.ec.email.value;
if (emailoutline.test(x))
{
alert("valid email");
}
}
that is part of a
print html
block so I think it should work fine. The html part works fine if I use it outside of the perl document, but once I link it there, it does not work anymore. The "#" in the regular expression is also marked green while all the rest of the html part of the script is marked red - I think the problem is that the "#" is interpreted by perl as the beginning of an array. Does anybody have an idea how to fix this problem?
This is the whole code:
#!/usr/bin/perl
use strict;
use warnings;
print "Content-type: text/html\n\n";
print <<'HTML';
<html>
<head>
<title>Mailcheck</title>
</head>
<body>
<form name="ec" action ="">
Email: <input type="text" name="email"> <br>
<input type="button" value="Pruefen" onclick="javascript:emailcheck();">
</form>
<script language="javascript" type="text/javascript">
function emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+\#[a-z0-9.-]+\.[a-z]{2,4}$/;
var x = document.ec.email.value;
if (emailoutline.test(x))
{
alert("This is a valid eMail");
}
else
{
alert("This is not a valid eMail");
}
}
</script>
</body>
HTML
exit;
I solved the problem on my own:
I had to rewrite the regular expression like this:
var emailoutline = /^[a-z0-9._%+-]+\#[a-z0-9.-]+\.[a-z]{2,4}\$\/\;
since perl cut of the reserved symbols "$" and "/". Now the code is working fine - Just wanted to post the solution if someone else ever comes across the same problem. Thank you for all the help though :)
In Perl, # is a reserved character. You need to escape it (\#) to avoid being interpreted as an array sigil.
Use a non-interpolating print to prevent Perl from attempting to expand variables:
print <<'HTML';
...
HTML
Related
Question :-
Write a javascript function to check whether a word or a sentence is palindrome or not irrespective of case and spaces. Name the HTML file as palin.html.
Give appropriate alerts on click of a button name is "palinbtn". Also provide a text box named "palin" which accepts the word / sentence.
Important Note :
Remove all white spaces from the input given and check for palindrome of the same input ignoring case.
After displaying the appropriate message through alert(), the page should not get redirected.
Do not use 'let' or 'const' keywords. Instead, use 'var'.
Use getElementById() or getElementsByName() to fetch value out of the HTML components.
Make sure all tags and attributes are in lower case
Code:-
<!DOCTYPE html>
<html>
<body>
//input from user using form
<form onsubmit="return display();">
Enter word/sentence to check for palindrome:<input
type="text"
name="palin"
id="palin"
/><br />
<input type="submit" name="palinbtn" value="Check Palindrome" />
</form>
<script>
function display() {
//getting the value from textbox
var str = document.getElementById("palin").value;
//removing special char. and converting to lowercase
var str = str.replace(/\s/g, "").toLowerCase();
//removing whitespaces
var input = str.split();
//joining the reversed string
var output = input.reverse().join("");
if (str == output) {
alert("The entry is a Palindrome.");
return false;
} else {
alert("The entry is not a palindrome");
return false;
}
}
</script>
</body>
</html>
Error in output
You need to split with an empty string to get all characters.
var input = str.split('');
// ^^
After displaying the appropriate message through alert(), the page should not get redirected.
alert(); is not used in this code
you could have an even simpler code :
if( str == str.toLowerCase().split('').reverse().join(''))
I'm trying to make a programming language that takes the input code, which uses String.replace to slowly translate bits of the new language into JavaScript, adding regex escapes. However, when I finally print out what I have at the end, it keeps the escapes, and will totally mess up the translated code. Here is what I have now:
<!DOCTYPE html>
<html>
<head>
<title>Translating</title>
</head>
<body>
<script type="text/javascript">
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
var infile = "{{ }}";
infile = escapeRegExp(infile);
infile = infile.replace("[", "{");
infile = infile.replace("]", "}");
document.write(infile);
</script>
</body>
</html>
The escapeRegExp is the function I'm using to escape the code in the first place. The infile variable will probably be filled by PHP when I am finished, but for now it just has test code.
I thought of a very simple way to fix this: just use infile = infile.replace(/\\/g, ""); and it will replace all backslashes with null strings.
This is my code
<!DOCTYPE html>
<html>
<script>
function isSpclChar() {
var iChars = "!##$%^&*()+=-[]\\\';,./{}|\":<>?";
if (document.getElementById('edit_tagline').value.indexOf(iChars) != -1) {
alert("The box has special characters. ");
} else {
alert("No special character");
}
}
</script>
<body>
<input type="text" id="edit_tagline" onblur="isSpclChar()">
</body>
</html>
My first question why this validation is not working?
My second question is can anybody explain me the meaning of this line
if(document.getElementById('edit_tagline').value.indexOf(iChars) != -1)
because I am new to JavaScript and copy paste the code from somewhere I don't know it's working.
So kindly if anyone could explain me the working of this code?
iChars is't a character range, it's treating it as a single word. You're going to need to implement a regular expression in order to do this.
Here's a working example of some validation in javascript, leveraging jQuery for brevity. Hopefully you can get it working in your context. I took the liberty of using alphabet characters and negating the expression, vs. writing out all of the potential special characters (there are hundreds including unicode ranges!).
$('button').click(function () {
if (!(/^[a-zA-Z0-9]+$/i).test($('#edit_tagline').val())) {
alert("The box has special characters. ");
} else {
alert("No special character");
}
});
HTML:
<input type="text" id="edit_tagline">
<button>Test</button>
Working example: http://jsfiddle.net/remus/9jwGW/
If you're not familiar with regular expressions, best read up on how those work (and check out http://regex101.com) before you get much further.
I'm having a problem with a javascript regex that has to comment out all tags inside a script tag. But it can not comment out special first script tag with id "ignorescript".
Here is a sample string to regex:
<script id="ignorescript">
var test = '<script>test<\/script>;
var xxxx = 'x';
</script>
Script tag inside ignorescipt has extra backslash because it is JSON encoded (from PHP).
And here is the final result i have to get:
<script id="ignorescript">
var test = '<!ignore-- <script>test<\/script> ignore-->;
var xxxx = 'x';
</script>
Following example works:
content = content.replace(/(<script>.*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
But I need to check that it does not contain a keyword "ignorescript". If that keyword comes up then I do not want to replace anything. Otherwise add ignore comments to whole script tag So far I have gotten this far:
content = content.replace(/(<script.((?!ignorescript).)*<\/script>)/g,
"<!--ignore $1 ignore-->");
It kinda works, but not the way it supposed to be. I also have one more backslash in ending tag. So I changed it to:
content = content.replace(/(<script.((?!ignorescript).)*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
Not it does not find anything at all.
Got it finally working.
Here is the working regex:
/(<script(?!\sid="ignorescript").*?<\\\/script>)/g
I have not found a good solution: I have a text box in which users need to be able to type specific info into. For example the command might be "9030 OUT FU [1234 TEST]". I need to "scrub" this text box to ensure that the data was typed in exactly this format (caps not necessary). However there are approximately 50 of these different types of commands.
I am fairly new to javascript, but with good direction can understand it. Is this possible with javascript? Once the data is entered into the text box, it will run a function to return some information, and the text box will be clear for the next command. No 2 commands can be entered at the same time. I just need to check the format is 100% accurate for each command. Any help is appreciated, thank you.
<script type="text/javascript">
function scrub(text) {
var commands = new Array{"someCommand","anotherCommand",...};
for (var i = 0; i <= commands.length; i++) {
if (text.value.toLowerCase().equals(commands[i])) {
//command is valid; do something here
} else {
alert("Invalid command");
}
}
text.value = ""; //clears the text box
}
</script>
For your textarea do this:
<textarea onblur="scrub(this);" ...></textarea>
Is there a set of keywords? And can be they be combined only in a certain fashion?
Looks like couple of regex patterns will be able to do the trick.
e.g: to match "9030 OUT FU [1234 TEST]" regex would be: /\d{4} OUT FU \[\d{4}\]/.
OUT FU and can be substituted with \w{3} and \w{2} respectively (unless you do not want any word to be allowed).
Use regular expressions.
html:
<input type="text" id="code" />
<input type="button" value="test" onclick="alert(checkCode())" />
javascript:
function checkCode(){
var code = document.getElementById('code').value;
return code.match(/\d+ \w+ \w+ \[\d+ \w+\]/)!=null ? true : false;
}
http://gskinner.com/RegExr/ is very helpful with regular expressions.
When you say "exactly this format", you have to understand that we have no clue what you mean. There are an infinite number of patterns that could be used to describe your example. The regular expression above will match if the code has a string of numbers, then a word, then another word, then an opening bracket, then a string of numbers, then a word, then a closing bracket.