Posted by: prajapatinilesh | October 29, 2008

sort textbox validation javascript

<script language=”javascript”>
//Function to trim the space in the left side of the string
function ltrim ( s ){
return s.replace( /^\s*/, “” );
}
//Function to trim the space in the right side of the string
function rtrim ( s ){
return s.replace( /\s*$/, “” );
}
//Function to trim the space in the string
function trim(s) {
var temp = s;
return temp.replace(/^\s+/,”).replace(/\s+$/, “” );
}
function checkSortOrderValue() {
inputEleArr = document.getElementsByTagName(‘input’);
isFillupSortText = false;
if (inputEleArr && inputEleArr != ‘undefined’) {
for (eltIndex = inputEleArr.length – 1; eltIndex >= 0; eltIndex–) {
currentElt = inputEleArr[eltIndex];
if (currentElt && currentElt != ‘undefined’) {

var eltTag = currentElt.tagName;
var eltType = currentElt.type;
var eltId = currentElt.id;
eltTag = (eltTag) ? eltTag.toLowerCase() : ”;
eltType = (eltType) ? eltType.toLowerCase() : ”;

//for sortbox only
if (eltType == ‘text’ && eltId) {
//sort text box have following id — sortText[uniqueIdVal here][sortOrder]
if (eltId.search(/sortText/) >= 0 ) {
eltValue = currentElt.value;
if (trim(eltValue)) { //trim is user defined function of javascript

isFillupSortText = true;
var anum=/(^\d+$)|(^\d+$)/ //only interger value allowed
if (anum.test(eltValue)) {
sortVal = parseInt(eltValue);
if (sortVal < 0 || sortVal > 32767 ) {
alert(“Sort number must be less than 32767.”);
return false;
}
}
else {
alert(“Invalid sort number. Please insert a valid sort number.”);
return false;
}
}//End of if (trim(eltValue))
}
}//End of if (eltType == ‘text’ && eltId)
}//End of if (currentElt && currentElt != ‘undefined’)
}//End of for (eltIndex = inputEleArr.length – 1; eltIndex >= 0; eltIndex–)
}

if (!isFillupSortText) {
alert(‘Please fillup one sort box value before pressing sort button.’);
return false;
}
return true;
}//End of funciton checkSortOrderValue
</script>
//html code like below….
<input type=”image” name=”sortBtn” id=”sortBtn” src=”sort.gif” width=”60″ height=”20″ onclick=”return checkSortOrderValue();”>
<?
//here $dataArr is the array values getting from the database.
foreach ($dataArr as $key => $valArr) {
$uniqueId = $valArr[‘id’];
$sortOrder = $valArr[‘sortOrder’];
?>
<input type=”text” name=”sortText[<?= $uniqueId ?>][sortOrder]” id=”sortText[<?= $uniqueId ?>][sortOrder]” value=”<?= $sortOrder ?>” size=”3″>
<? } ?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: