validation for particular fields like input name="",textarea,select name="".

   <form method="post" id="myform" name="myform">
 <tr><td width="170"><div align="left">Company Name<span style="color:#F00;">*</span> :</div></td><td width="317"> <input type="text" name="companyname" placeholder="Company Name" value="<?php echo ucwords(strtolower(mysql_real_escape_string($_POST['companyname']))); ?>"/></td></tr>
        <tr><td><div align="left">Contact Person Name<span style="color:#F00;">*</span> :</div></td><td> <input type="text" name="conactperson" placeholder="Contact Person Name" value="<?php echo ucwords(strtolower(mysql_real_escape_string($_POST['conactperson']))); ?>"/></td>
        </tr>
        </form>

company name is mandatory but if i go to next fields i want to display one message near that companyame text box. am searching for this still not getting.if anyone knows about this validation please help me.

Dani AI

Generated

asked for an inline validation message to appear as focus leaves the company-name field; pointed toward the jQuery Validation plugin for full-form rules. For a simple, immediate solution that runs before submit, combine a small client-side empty/format check with an optional debounced AJAX call to a server endpoint for checks the browser can't do (uniqueness, business logic). The server must always re-check on submit.

Minimal client-side pattern (bind on input/blur, debounce network calls, write message into an aria-live span):

<input id="companyname" name="companyname" type="text" aria-describedby="companyname-msg" />
<span id="companyname-msg" class="field-msg" aria-live="polite"></span>
$(function(){
  var timer;
  $('#companyname').on('input blur', function(){
    var $el = $(this);
    clearTimeout(timer);
    timer = setTimeout(function(){
      var val = $el.val().trim();
      var $msg = $('#companyname-msg');
      if (!val) {
        $msg.text('Company name is required').addClass('error');
        $el.attr('aria-invalid','true');
        return;
      }
      // optional server check
      $.ajax({
        url: 'validate_company.php',
        method: 'POST',
        dataType: 'json',
        data: { companyname: val }
      }).done(function(res){
        if (res.valid) {
          $msg.text('').removeClass('error');
          $el.removeAttr('aria-invalid');
        } else {
          $msg.text(res.message || 'Invalid').addClass('error');
          $el.attr('aria-invalid','true');
        }
      }).fail(function(){
        $msg.text('Validation failed').addClass('error');
      });
    }, 300); // 300ms debounce
  });
});

Server endpoint example (returns JSON {"valid":true/false,"message":"..."}). Use PDO prepared statements rather than deprecated mysql_* calls and sanitize/validate on the server:

<?php
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['valid'=>false]); exit; }
$name = trim($_POST['companyname'] ?? '');
if ($name === '') { echo json_encode(['valid'=>false,'message'=>'Company name required']); exit; }

try {
  $pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','dbuser','dbpass', [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
  $stmt = $pdo->prepare('SELECT 1 FROM companies WHERE companyname = ? LIMIT 1');
  $stmt->execute([$name]);
  $exists = (bool) $stmt->fetchColumn();
  echo json_encode($exists ? ['valid'=>false,'message'=>'Name already used'] : ['valid'=>true]);
} catch (Exception $e) {
  http_response_code(500); echo json_encode(['valid'=>false,'message'=>'Server error']);
}

Notes: keep AJAX calls debounced, include CSRF token in requests for security, mark inputs with aria-invalid and use aria-live for announcements, and provide an HTML5 required fallback so the form behaves when JS is disabled. For full-form rule sets and nicer messages consider the jQuery Validation plugin (as suggested), but always duplicate client checks on the server before accepting data.

Recommended Answers

All 2 Replies

thank you.. before clicking submit button validation method i want if that field is mandatory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.