<strong>This code have</strong>
<strong>1. <span style="color: #ff00ff;">Remove un-wanted charecters</span></strong>
<strong>2.<span style="color: #ff00ff;">Remove whitespace start and last</span> in javascript + php</strong>
<strong>3.<span style="color: #ff00ff;">Validation alpha numeric</span> in javascripts + php</strong>
<span style="color: #ff00ff;"><strong>For run</strong></span> Please select All code and paste it then run it:
< ?php
//validations:
//1.remove unwanted charecters
//2.remove whitespace start and last in js + php
//3.validation alpha numeric in js + php
function validateAlphaNumeric($v_username)
{
return eregi('[^a-z0-9_ ]', $v_username) ? FALSE : TRUE;
}
//print_r($_POST);
$name = $_POST[name];
$email = $_POST[email];
$ErrorName="";
echo "<br />name remove white space first and last=".$name = preg_replace("/^s+|s+$/","",$name);
echo "<br />name remove unwanted garbage=".$name = preg_replace("/&amp;amp;(^;)+;/","",$name);
if($_POST['send'] == "Send Message" )
{
if(validateAlphaNumeric($name))
{
//Passed
}
else
{
$ErrorName="Error: name Only Alppha Numeric underscore whitespace Required!!";
}
}
?>
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
// CSS goes here
</style>
<script language="javascript" type="text/javascript">
function alphanumeric(alphane)
{
//http://www.hscripts.com/scripts/JavaScript/alphanumeric-check.php
var numaric = alphane;
for(var j=0; j<numaric .length; j++)
{
var alphaa = numaric.charAt(j);
var hh = alphaa.charCodeAt(0);
if((hh > 47 &amp;amp;&amp;amp; hh&amp;amp;lt;58) || (hh > 64 &amp;amp;&amp;amp; hh&amp;amp;lt;91) || (hh > 96 &amp;amp;&amp;amp; hh&amp;amp;lt;123) || (alphaa == "_")|| (alphaa == " "))
{
}
else
{
return false;
}
}
return true;
}
function trim(str)
{
//http://www.php-mysql-tutorial.com/wikis/php-tutorial/form-validation-using-php.aspx
return str.replace(/^s+|s+$/g,'');
}
function jayjava1()
{
/*The name values is trimmed to remove extra spaces from the beginning and end of the name. If you do not enter your name or only entering spaces then an alert box will pop up.
The forward slash (/) is used to create a regular expression. Note that it is not a string, you don't have to use quotes and it won't work if you use quotes.
* ^ : the beginning of a string
* $ : end of string.
* s : single whitespace character (tab also count as whitespace)
* + : one or more
* | : conditional (OR)
* g : global, mainly used for search and replace operation
So in english the search replace function above can be read as :
"Replace one or more whitespace character from the beginning or ending of a string with blank character"*/
var errMsg = "";
if(trim(document.form1.name.value) == '')
{
//alert('Please enter your name');
errMsg+=' Error: Name Required !!';
//document.form1.name.focus();
//return false;
}
if(trim(document.form1.name.value) != '')
{
if(document.form1.name.value.length < 10)
{
//alert('len='+document.form1.name.value.length);
errMsg+='n Error: Name more then 10 character required!!';
//alert('Name more then 10 character required');
//document.form1.name.focus();
//return false;
}
}
if(trim(document.form1.name.value) != '')
{
if(alphanumeric(document.form1.name.value))
{
// errMsg+='nPassed alphanumeric';
}
else
{
errMsg+='n Error: Name Only Alppha Numeric underscore whitespace Required !!';
//alert('Name Only Alppha Numeric underscore Required');
//document.form1.name.focus();
//return false;
}
}
if(trim(document.form1.email.value) == '')
{
//alert('Please enter your email');
errMsg+='n Error: Email required !!';
//document.form1.email.focus();
//return false;
}
var email=document.form1.email.value;
var emailFormat = /^w(.?[w-])*@w(.?[w-])*.[a-zA-Z]{2,6}(.[a-zA-Z]{2})?$/i;
if(email!= "")
{
var eres=email.search(emailFormat);
if(eres == -1)
{
//alert("Please Enter Valid Email..");
errMsg+='n Error: Email valid email required.. !!';
//document.form1.email.focus();
//return false;
}
}
if(errMsg != "")
{
alert(""+errMsg);
return false;
}
}
</script>
</numaric></script></meta></head>
<body>
<form method="post" name="form1" onSubmit="return jayjava1();">
<table width="800" border="0" align="center" cellpadding="2" cellspacing="1" class="maincell">
<tr>
<td width="106">Your Name</td>
<td width="381">
<input name="name" type="text" value="<?php echo $name; ?/>">&amp;amp;nbsp;< ?php if($ErrorName != "") { echo $ErrorName;} ?>
</td></tr>
<tr>
<td>Your Email</td>
<td>
<input name="email" type="text" size="30" value="<?php echo $email; ?/>">
</td></tr>
<tr>
<td>Subject</td>
<td><input name="subject" type="text" class="box" id="subject" size="30"/></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message" cols="55" rows="10" wrap="OFF" class="box" id="message"></textarea></td>
</tr>
<tr align="center">
<td colspan="2"><input name="send" type="submit" class="bluebox" id="send" value="Send Message"/></td>
</tr>
<tr align="center">
<td colspan="2">&amp;amp;nbsp;</td>
</tr>
</table>
</form>
</body>
</html>
<strong>
</strong>
Page views:1299