Jump to content

Recommended Posts

Hello

I have a textbox and a input button which will insert a mysql record, however i need to validate that textbox if its empty it shouldnt allow that button to insert any record but show a popup error.

my script :
 

<script Language="JavaScript">

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

</script>

Form :
 

<form name="frmContact" id="frmContact" method="POST" action="index.php" onsubmit="return Blank_TextField_Validator()">

however its not validating or showing any error. any help?

Edited by sam20e
Link to comment
https://forums.phpfreaks.com/topic/292410-js-validation/
Share on other sites

OK, but you know you still need to add validation on the server-side too, right? Client-side validation is a great tool, but it should never be relied upon since it can be easily circumvented.

 

In your code above, you are referencing "frmContact" in the function, but it is not defined. You can pass the form reference in the onsubmit call. Also, you should trim the value to ensure it doesn't pass with just spaces entered.

 

 

<html>
<head>
<script>
 
function Blank_TextField_Validator(formObj)
{
    //Trim the value in the field
    formObj.id.value = formObj.id.value.trim();
    // Check the value of the element named text_name
    // from the form named text_form
    if (formObj.id.value == "")
    {
        // If empty display and alert box
        alert("Please fill in the text field.");
        // Place the cursor on the field for revision
        formObj.id.focus();
        // return false to stop further processing
        return false;
    }
    // If text_name is not null continue processing
    return true;
 
}
 
</script>
</head>
 
<body>
 
<form name="frmContact" id="frmContact" method="POST" action="index.php" onsubmit="return Blank_TextField_Validator(this)">
 
<input type="text" name="id" id="id">
<button type="submit">Submit</button>
</form>
 
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/292410-js-validation/#findComment-1496334
Share on other sites

Thank you for your prompt response. I adjusted my code but its still letting empty records to be saved.

 

1) I channged formObj to my form id

2) I changed id to another column/field name

 

Apart from these 2 changes I copy/paste your new codes, so it should work right? am I making any errors? If you have any other JS scrtips to validate easily can you share with me?

Link to comment
https://forums.phpfreaks.com/topic/292410-js-validation/#findComment-1496359
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.