function valideraAmount() {
var f = document.forms[0];
var a = parseInt(f.amount.value, 10); // 10 base
if (!isNaN(a) && a >= 100) {
f.amount.value = a;
} else {
alert('Minsta belopp 100 kr.');
return false;
}
} Filter
2004-04-20
Validera heltal
Javascript
2004-04-20
Trimma strängar
Javascript
function trim(str) {
while (str.charAt(0) == " ") {
str = str.substr(1);
}
while (str.charAt(str.length - 1) == " ") {
str = str.substring(0, str.length - 1);
}
return str;
}
// Bättre alternativ:
function trim(s) {
return s.replace(/(^\s+)|(\s+$)/g, "")
} 2004-04-17
Enkel mail i php
Mail
<?
$to = ""; // email
$from = ""; // email
$subj = "";
$msg = "";
$resp = "/index.php"; // response page
if (isset($_POST["to"])) { $to = $_POST["to"]; }
if (isset($_POST["from"])) { $from = $_POST["from"]; }
if (isset($_POST["subj"])) { $subj = $_POST["subj"]; }
if (isset($_POST["msg"])) { $msg = $_POST["msg"]; }
if (isset($_POST["resp"])) { $resp = $_POST["resp"]; }
mail($to, $subj, $msg, "From: " . $from);
header("Location:$resp");
?> 2004-04-12
Rekursivt träd
Php
<html>
<head>
<title>tree</title>
<style type="text/css"><!--
body { font-family: Verdana; font-size: 11px }
a:link { color: 000000; text-decoration: none}
a:visited { color: 000000; text-decoration: none}
a:hover { color: 0066ff; text-decoration: none }
--></style>
</head>
<body>
<?
$path = './';
global $indrag;
$indrag = -1;
function open_dir($path) {
global $indrag;
$indrag++;
$handle = opendir($path);
while ($file = readdir($handle)){
if($file != "." && $file != '..') {
echo str_repeat(" ", $indrag);
if (is_dir($path . $file)) {
echo ' /' . $file . '/<br />' . "\n";
open_dir($path . $file. '/');
$indrag--;
} else {
echo '<a href="' . $path . $file . '">' . $file . '</a><br />' . "\n";
}
}
}
closedir($handle);
}
open_dir($path);
?>
</body>
</html> 2004-04-12
Enkel session log-in
Php
<?
session_start();
$cmd = "";
$pass = "";
if (isset($_POST['cmd'])) { $cmd = $_POST['cmd']; }
if (isset($_POST['pass'])) { $pass = $_POST['pass']; }
if ($cmd == "Logga in" && $pass == "test") {
$_SESSION['adm'] = 1;
} else if ($cmd == "Logga ut") {
$_SESSION['adm'] = 0;
} ?>
<html>
<head>
<title>Log in</title>
</head>
<body>
<table border="0" cellpadding="2" cellspacing="0" align="center">
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<tr align="center">
<td>
<? if (isset($_SESSION['adm']) && $_SESSION['adm'] == 1) { ?>
<input type="submit" value="Logga ut" name="cmd">
<? } else { ?>
<input type="password" name="pass"><br>
<input type="submit" name="cmd" value="Logga in">
<? } ?>
</td>
</tr>
</form>
</table>
</body>
</html> 