/    Sign up×
Community /Pin to ProfileBookmark

Validate empty space

Hi

I have a php form and an insert SQL statement on button click. its working great however the form even inserting empty records, therefore i need a control over there to validate empty textbox space

[CODE]form name=”frmContact” id=”frmContact” method=”POST” action=”index.php” onsubmit=”return Blank_TextField_Validator()”>[/CODE]

and my javascript :

[code]
<script Language=”JavaScript”>

function Blank_TextField_Validator()
{
// Check the value of the element named text_name
// from the form named text_form
if (frmContact.cid.value == “”)
{
// If null display and alert box
alert(“Please fill in the text field.”);
// Place the cursor on the field for revision
frmContact.cid.focus();
// return false to stop further processing
return (false);
}
// If text_name is not null continue processing
return (true);
}

</script>
[/code]

Its not validating and not showing any alerts. any help?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 12.2014 — For a more general purpose function consider this. Following function is untested...

<i>
</i>&lt;script type="text/javascript"&gt;

function Blank_TextField_Validator(info) {
info = info.replace(/s*/g,''); // check for " " or " ", etc., entries // OPTIONAL TEST

// Check the 'info' parameter value of the element named text_name from the form named text_form
if (info == "") {
alert("Please fill in the text field."); // If empty field, display an alert box
return false; // return false to stop further processing
} return true; // If info is not blank, continue processing
}

var elem = frmContact.cid;
if (elem == null) { alert('NULL or UNDEFINED'); elem.focus(); } // should never get here
if (Blank_TextField_Validator(elem.value) == false) { elem.focus(); } // entered blank value in element

&lt;/script&gt;
×

Success!

Help @sam20e spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...