I have a form into which the user types a node ID. In the form validation, I want to check that it is a valid ID (i.e. it is a node and of the right type).

How would I do that?

Accepted answer

If you want to avoid loading the full node, you can do

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')->propertyCondition('nid', $my_nid);

$result = $query->execute();

if (empty($result['node'])) {

  // The NID doesn't exist.

Most helpful answers

Nothing wrong with loading a node. The query is just slightly more efficient. As another alternative, you can use db_query on the node table as well. Again, slightly more efficient than EntityFieldQuery.

Comments

Nvm. Figured it out. I can just node_load and if it returns false, it doesn't exist. Then I can check the node type. 

If you want to avoid loading the full node, you can do

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')->propertyCondition('nid', $my_nid);

$result = $query->execute();

if (empty($result['node'])) {

  // The NID doesn't exist.

That is good to know. I suppose it's probably good to avoid loading nodes that I won't end up needing. :)

Nothing wrong with loading a node. The query is just slightly more efficient. As another alternative, you can use db_query on the node table as well. Again, slightly more efficient than EntityFieldQuery.

Can I ask what does the node id represent content wise?

Could you use Entity Reference and then as the user types the NID or stock code or title etc.. they would see the node title for example...  would that be a more user friendly solution?

@DrAlbany - possibly. It's just site admin using it to update nodes, so I'm not too concerned about user-friendliness. Just wanted some basic error checking so I could (e.g.) loop over all the nodes, for example.